0

I try to insert comment via REST API , but i get this error :

Query parameter not permitted: author

I followed this docs and author is one of the arguments this is the link test that i use

https://www.myblog.com/wp-json/wp/v2/comments/?author=1&content="TestComment"&post=1

So, what i am doing wrong ?

1
  • 1
    Creating comments via the REST API requires authentication, and the authenticated user also needs the edit_posts capability - i.e. permissions to edit posts - in order to use the author parameter.
    – Sally CJ
    Sep 13, 2019 at 0:50

1 Answer 1

1

I do not recommend doing this. It's better if you authenticate your users, but you can make an api endpoint that posts a comment like this:

add_action('rest_api_init',
    function () {
    register_rest_route(
        'api',
        'api_post_comment',
        array(
            'methods'  => 'GET',
            'callback' => 'api_post_comment',
        )
    );
});

function api_post_comment() {
    wp_insert_comment([
        'comment_approved' => 0,
        'comment_author' => 'test',
        'comment_author_email' => '[email protected]',
        'comment_author_IP' => $_SERVER['REMOTE_ADDR'],
        'comment_content' => 'test',
        'comment_parent' => 0,
        'comment_post_ID' => 25084,
    ]);
}

You can learn more here: https://developer.wordpress.org/reference/functions/wp_insert_comment/

and here: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.