question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to insert User meta?

See original GitHub issue

Hi, thank for that library!

I did not really understand the structure of the meta parameter. Could you please give me an example of it?

i’ m trying to insert a new user. I create this function

        {
            WordPressClient client = await GetClient();
            var isValidToken = await client.IsValidJWToken();
            if (isValidToken)
            {
                var newUser = new User("example", "example@example.it", "password")
                {
                    FirstName = "examplename",
                    LastName = "examplesurname",
                    NickName = "nick",
                    Meta = ????
                };
                Console.WriteLine("Start");
                var insUser = await client.Users.Create(newUser);
                Console.WriteLine("Done");
            }
        }

but i don’ t know how insert “Meta”. I must insert that beacuse i use Ultimamate Member plugin and all extra information of a user are insert as meta.

What am i doing wrong?

Thanks for the attenction.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
LSparkzwzcommented, Aug 31, 2019

I’m also in the same situation, I tried doing something like this but it doesn’t work: Meta = { meta_field1 = "something1", meta_field2 = "something2" } Assuming the field names are the ones you can read in the database, how can I structure this meta field correctly?

1reaction
carlford10scommented, Jan 20, 2020

This worked for me:

Create a class with the fields to work with:

    public class UserMeta 
    {
        public string _full_name { get; set; }
        public string _company_name { get; set; }
        public string _company_website { get; set; }
    }

Then create an instance of the WordPressPCL.Models.User Class

            var wpUser = new WordPressPCL.ModelsUser();

Populate non-meta fields for example:

            wpUser.Email = Input.Email;
            wpUser.FirstName = Input.FirstName;

Then create an instance of the wpUser.Meta class using the UserMeta class defined above and populate those fields:

            wpUser.Meta = new UserMeta()
            {
                _company_name = Input.CompanyName,
                _company_website = Input.CompanyWebsite,
                _full_name = Input.FirstName + " " + Input.LastName
            };

This works because the Meta object within the WordPressPCL.Models.User class is defined as dynamic.

Now you can create or update the record (to update define the Id field of the user.)

            User wpRespond = new User();
            try 
            {
                client.JsonSerializerSettings = new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore };
                wpRespond = await client.Users.Create(wpUser);
            }
            catch (WPException wpex)
            {
                //var name = wpex.BadRequest.Name;
                //var message = wpex.BadRequest.Message
                //var wpex.BadRequest.Data
                throw wpex;
            };

Hope this helps

Read more comments on GitHub >

github_iconTop Results From Across the Web

add_user_meta() | Function - WordPress Developer Resources
Adds meta data to a user.
Read more >
How to Create and Use WordPress User Metadata ...
The function for either adding or updating WordPress user meta data is called update_user_meta() , and it looks as follows: update_user_meta( $ ...
Read more >
Wordpress: Create a new usermeta field for users
You can create a simple plugin to hook into the user profile actions and add a new field. To add the field to...
Read more >
How to Add Custom User Profile (User meta) Fields In ...
In the WordPress database, there is a table known as wp_usermeta which stores all extra information that is attached to the user profile....
Read more >
Add custom user meta data
global $wpdb; $sql_query = "INSERT INTO {$wpdb->usermeta} (user_id, meta_key, meta_value) " . "SELECT ID, 'YOUR_META_KEY', '' FROM {$wpdb->users ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found