WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
How to add a comment or post? (4 posts)

  1. theomanus
    Member
    Posted 2 years ago #

    Hi there,

    Im developing an mobile version of my website and I have a section, which gets all my post from my wp blog. This is working fine with this pugin.
    But I don't understand how to add a comment or a post via JSON.
    Please can anyone help me out here and show me an example of how to do it???

    I know from the documentation that I need the method submit_comment

    Method: submit_comment
    Submits a comment to a WordPress post.

    Required arguments
    post_id - which post to comment on
    name - the commenter's name
    email - the commenter's email address
    content - the comment content
    Optional arguments
    redirect - redirect instead of returning a JSON object
    redirect_ok - redirect to a specific URL when the status value is ok
    redirect_error - redirect to a specific URL when the status value is error
    redirect_pending - redirect to a specific URL when the status value is pending
    Custom status values
    pending - assigned if the comment submission is pending moderation

    I need an example code of how to call this method and to parse it via JSON.

    http://wordpress.org/extend/plugins/json-api/

  2. shivark
    Member
    Posted 2 years ago #

    I need the same info, were able to find a solutions?

  3. bbottema
    Member
    Posted 2 years ago #

    First you need a nonce (Number used ONCE), which is a temporary key you'll use to be able to invoke the API with:

    http://yourblog.com/?json=core.get_nonce&controller=posts&method=create_post&callback=?

    This gives you a nonce number (be sure to use &callback=? as it marks the content as jsonp, or you'll get a similar -but invalid- nonce).

    Then use this nonce to create a post:

    http://yourblog.com/?json=posts.create_post&nonce='+nonce+'&title='+title+'&content='+content+'&status=publish (or draft, or leave it out altogether)

    Make sure you have the 'posts' controller enabled in your wordpress plugin JSon API settings. Check this manual for what JSon data structures you can expect back from these calls.

    Now, here's the tricky part: you need to be already logged into the wordpress site, because with this JSon API, you can't log in. I haven't figured that part out yet, so I'm still looking for a good solution myself. I tried manually posting and also width ajax but with limited results considering I'm missing a WordPress test cookie in my headers (at least this is the main difference I see when logging in from the site and doing it manually)

  4. vbakare
    Member
    Posted 2 years ago #

    I modified the post.php controller file to authenticate using user name password.
    I wanted to create post from my intranet J2EE application , and was not allowed to retrieve actual passwords of the User from Active Directory.
    So I created a Super User who has access to create post just for authentication and modified the authenticate() method from post.php to read one more parameter 'wpAuthUser' and let the 'Author' parameter used for post author only.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    here is the URL to create post with login
    http://localhost/wordpress/api/?json=create_post&nonce=12313&status=publish&title=VB&content=VB_CONTENT&author=vb&user_password=admin&wpAuthUser=admin

    you can also refer below link for original concept.
    https://github.com/dphiffer/wp-json-api/pull/3

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags