Question

I'm trying to complete fields with JoeBlogs WordPress Wrapper.

My code is:

private void postToWordpress(string title, string postContent,string tags, string aioTitle)
{       
    string link = this.maskedTextBox1.Text;
    string username = this.maskedTextBox2.Text;
    string password = this.maskedTextBox3.Text;

    var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password);
    var post = new Post();

    post.Title = title;
    post.Body = postContent;
    post.Tags = tags.Split(',');

    string[] cf = new CustomField(); //{ ID = "name", Key = "aiosp_title", Value = "All in One SEO Title" };
    cf.ID = "name";
    cf.Key = "aiosp_title";
    cf.Value = "All in One SEO Title";

    post.CustomFields[0] = cf;

    wp.NewPost(post, false);
}

The error is at this line:

post.CustomFields[0] = cf;

And it is:

An unhandled exception of type 'System.NullReferenceException' occurred in JoeBlogsWordpressWrapperTests.exe

Additional information: Object reference not set to an instance of an object.

So, how to use/add correctly custom fields on WordPress from C# Application using JoeBlogs WordPress Wrapper?

Was it helpful?

Solution

The following code fixes your NullReferenceException and also successfully saves the custom fields into the Post in Wordpress.

private void postToWordpress(string title, string postContent,string tags, string aioTitle)
{       
    string link = this.maskedTextBox1.Text;
    string username = this.maskedTextBox2.Text;
    string password = this.maskedTextBox3.Text;

    var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password);
    var post = new Post();

    post.Title = title;
    post.Body = postContent;
    post.Tags = tags.Split(',');

    var cfs = new CustomField[] 
        { 
            new CustomField() 
            { 
                // Don't pass in ID. It's auto assigned for new custom fields.
                // ID = "name", 
                Key = "aiosp_title", 
                Value = "All in One SEO Title" 
            } 
        };

    post.CustomFields = cfs;

    wp.NewPost(post, false);
}

You were getting the NullReferenceException error because you were creating a string array and trying to assign it the CustomFields property of the Post object, which is an array of CustomField i.e. CustomField[].

Also, in order to save the CustomFields to the Post in the database, you should pass in only the Key and Value fields of the CustomField struct and skip the ID field all together. Reason being Wordpress auto-generates the ID fields (also it's an integer / numeric field in the database). I think that was what was causing the XmlRpc call to fail, but we did not get any errors as to why.

Try the above code and it should work (I have it working on my localhost WAMP Wordpress installation).

One final note. Although the CustomField's name property is called Key, it doesn't have to be unique, and uniqueness is not enforced. So for instance, if you are populating a custom dropdown box with a list of cities for a Post, you could have the list of cities as a set of custom fields as follows.

    var cfs = new CustomField[] 
        { 
            new CustomField() 
            { 
                Key = "aiosp_title", 
                Value = "All in One SEO Title" 
            } , 
            new CustomField() 
            { 
                Key = "this is another custom field with HTML", 
                Value = "All in One SEO Title <br/> Keyword 1 <br/><p>This is some more text and html</p>" 
            } ,
            new CustomField() 
            { 
                Key = "list_of_cities", 
                Value = "San Francisco" 
            } ,
            new CustomField() 
            { 
                Key = "list_of_cities", 
                Value = "New York" 
            } 
        };

This will also get saved to the post, with 2 custom fields with the same Key value and different text in the Value field's value.

And last but not least, you can store HTML also in the custom fields (as shown above).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top