wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
342 stars 130 forks source link

How to insert User meta? #163

Closed eBenassati closed 4 years ago

eBenassati commented 5 years ago

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.

ThomasPe commented 5 years ago

Is this still an issue?

hbcondo commented 5 years ago

Yes, I created the same issue before this one per https://github.com/wp-net/WordPressPCL/issues/160

LSparkzwz commented 5 years ago

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?

hbcondo commented 5 years ago

@ThomasPe, this is still an issue. Would you please review and provide us your feedback? Thank you.

nkuDev commented 5 years ago

I'm having the same Problem :/ Did someone find a solution for it ?

nkuDev commented 5 years ago

@hbcondo I found a solution for it, what you need to do is to set up the register_meta in your "function.php". And then just simply do it like i did it in the Image WordPress MetaDaten

I hope i could help you.

ThomasPe commented 4 years ago

@hbcondo @LSparkzwz @eBenassati did this solution work for you?

hbcondo commented 4 years ago

Is there a way to add / edit user meta data using only WordPressPCL?

carlford10s commented 4 years ago

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

Daraciel commented 1 year ago

Hi i tried @carlford10s solution but it doesnt work for me. Has something changed since 2020?