Creating a new post in WordPress using the JoeBlogs library

A few people have recently been asking how the NewPost method works within JoeBlogs

First, you need to create an instance of Post.

Then, set the following properties:

dateCreated
Fairly self explanatory, but you should set this to today’s date (or whatever date you wish the post to be set as published)

title
The title of the post

description
The body of the post.
This can of course contain HTML

categories
This is a string array of categories to associate with the post

mt_keywords
Another string array, representing the tags for the post

Then, using your presumably already instantiated Wrapper class, you can call the NewPost method, which takes the above Post object as a parameter, and a boolean – indicating if the post is to be set as published. Note – if this is set to false, the post is set in draft mode.

Here’s some sample code:

//create a new post
var post = new Post();

//since this is a struct, we can't have a constructor that does this!
post.dateCreated=DateTime.Now; 
post.title="This is a title";
post.description="this is the body of the post. it <strong>could</strong> be html.";

//create the post!
wp.NewPost(post,true);

Hope this helps!

32 responses to “Creating a new post in WordPress using the JoeBlogs library”

  1. Alex avatar

    I added this demo code to revision #32613 on CodePlex:
    http://joeblogs.codeplex.com/

  2. Bluka Tria avatar

    Hey Alex,

    I love your plugin and wanted to thank you for putting it together 🙂

    -Anonymous

  3. Stinky48 avatar

    As with any early software version under development, bugs are found and fixed as part of the testing and development. ,

    1. Alex avatar

      Exactly.
      This project is open source – if people find bugs and would like to fix, please let me know and I will add you as a contributor on codeplex 🙂

  4. Waseem avatar

    Thanks a million for sharing this info.

  5. Dave avatar
    Dave

    Thanks for putting this together, the WP documentation is sparse!

    Is there a way to set the slug, or to get it once the post is created? The postid is empty after I create the post.

  6. Joey avatar

    It looks like a while since you’ve supported this library. I concur with Dave, is there any way to get the post’s ID or slug after posting?

    1. Alex avatar

      Yeah, it’s been a while – I’m working on updating it at the moment though!

  7. Joey avatar

    Awesome! I’ll look for the update. Also, mt_keywords doesn’t seem to have a definition in the post struct.
    BTW, excellent library, thanks.

  8. Brad avatar
    Brad

    Could you post a code example of using mt_keywords to set post tags for a new post in C#?

  9. Alex avatar

    That’s probably caused by not having XML-RPC enabled on your blog
    In wp-admin, look in Writing settings, and enable the checkbox marked:

    Enable the WordPress, Movable Type, MetaWeblog and Blogger XML-RPC publishing protocols.

  10. Dremation avatar

    Yes, this is enabled and working with other services.

  11. Webdev avatar

    I just installed WordPress Ver 3.0.1. And I’m trying to post for the first time using the JoeBlogs Version 1 library. I’m getting error message:

    “Response from server does not contain valid XML.”

    And further error message detail says that:

    “There are multiple root elements. Line 2, position 2.”

    I have XML-RPC enabled in WordPress. And it’s not the (“xmlprc.php – this should be xmlrpc.php”) spelling issue.

    I’m using the default installation with no add-ons.

    I’m thinking that my problem is an incorrect WordPress setting.

    Any assistance from anyone would be greatly appreciated.

    Thank you in advance.

  12. Waseem avatar

    Could you post a code example of using mt_keywords to set post tags for a new post in C#?

  13. Pawan avatar
    Pawan

    Hi Alex, Any sample showing how to add post tags and custom fields for post?

  14. Steve avatar
    Steve

    Bump.
    I would really like to use it, because the other implementation out there (wordpress.Net) requires recompiling every time you want to access a different blog. But I have 2 problems.
    1) mt_keywords does not seem to be recognized (not there is intellisense)
    2) custom fields does not seem implemented. (ditto)

    I’m not sure if the source code on codeplex is a later version – but I could not get it to compile – but that may just be me.

    Any chance you will fix these – I would make it a very useful tool. Unfortunately at the moment I am stuck with 2 partial implementations and really don’t understand enough of what is going on to fix it myself.

    Guess its back to WordPress.Net for now – but hope you can help.

    Thanks

  15. Daniel avatar

    you have to set categories as an int[] array. If there is a new category, you have first to add it. Or is there another way?

  16. Manish Choudhary avatar

    Is it possible to get all post from my wordpress blog?

  17. DaveCS avatar

    @Daniel and others having problems with categories.

    You have to set the categories explicitly by name. The category id shown in the code examples don’t seem to work.
    Browsing the object browser on his assembly is much better than the documentation, give it a try.

    Example in VB. Use the online converter at developerfusion if you don’t know VB and coding in C#.

    Private Sub btnPost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPost.Click
    Dim Url As String = “http://somesite.info/xmlrpc.php”
    Dim User As String = “Poster123”
    Dim Password As String = “password123”
    Dim wp = New WordPressWrapper(Url, User, Password)
    Dim post = New Structs.Post()
    post.dateCreated = DateTime.Now
    post.title = “This is a title test”
    post.description = “This is post content test could be html.”

    ‘This would post only in Free Movie Downloads
    ‘post.categories = {“Free Movie Downloads”}

    ‘This would post in Free Movie Downloads and New
    post.categories = {“Free Movie Downloads”, “New”}
    ‘create the post
    wp.NewPost(post, True)
    End Sub

  18. DaveCS avatar

    Whoa…. I just checked later versions of this.

    My code works with version 1, the version showcased on the codeplex site. http://joeblogs.codeplex.com/

    In later versions, the version that allows comment posting, the namespace and some method types change.

    No wonder why everyone is confused. The first released assembly has a very different fundamental structure than the current release. In a bad way…. The discussion code snips and examples might work with a certain “Revision” and completely fail with another. All depending on who is using what version…. LOL Wow, talk about throwing a wrench in things.

  19. Terrell avatar
    Terrell

    This is an awesome wrapper Alex, Thanks a lot! This a real time saver.

  20. Richard avatar

    Hello Alex,

    When I submit a post using newPost(post,true) the post_id is incrementing by two from the previous last post.

    So from 1 the next post id would be 3, any ideas? Any one had the same problem as me?

    Thanks for the wrapper, its absolutely awsome btw.

    Thanks.

    Richard.

    1. Alex avatar

      Strange…
      Please could you submit that as an issue on the github page?

      1. Richard avatar

        Hello Alex,

        Not to worry about this, I realise that its beacuse wordpress was adding the posts as scheduled due to the server times being out of sync. Meaning it was adding a draft post and the original hence incrementing by two. All coo!

        Have notice another issue however when using your wrapper. Which may be of interest:

        When returning getrecentPosts(x); and getPages(); wordpress is returning string values for postid and pageid, where as your wrapper is expecting an integer. So I have had to edit the following setting inside wordpress /wp-includes/class-wp-xmlrpc-server.php

        Line 530:
        ‘wp_author_id’ => (string) $author->ID,
        to
        ‘wp_author_id’ => (int) $author->ID,

        AND

        Line 2948
        ‘postid’ => (string) $entry[‘ID’],
        ‘postid’ => (int) $entry[‘ID’],

        I am using wordpress version 3.3.1.

        All the best,

        Richard.

  21. pppz avatar

    How to get post url? Is it possible now?

  22. Kien avatar
    Kien

    How to add a custom field?

    1. Alex Brown avatar

      Not yet supported…

  23. Muratcan Güler avatar
    Muratcan Güler

    how to add a custom field ?

    1. Alex Brown avatar

      Not yet supported…

  24. Nazım Hikmet Öztürk avatar
    Nazım Hikmet Öztürk

    Hello. how to send image as Set Thumbnail image to wordpress?

Leave a Reply

Your email address will not be published. Required fields are marked *