admin管理员组

文章数量:1129103

I'm trying to post a comment via the WP REST API. Here is my request:

axios({
    method: 'post',
    url: `${WP_API_ENDPOINT}/comments`,
    headers: {'X-WP-Nonce': wpRestSettings.nonce},
    data: {
        "post": 8911,
        "author_name": "Mr. Author",
        "author_email": "[email protected]",
        "content": "A new comment."
    }
})

I've also tried adding the nonce in the data instead under the value _wpnonce per the spec.

I saw one answer suggest adding this filter: add_filter( 'filter_rest_allow_anonymous_comments', '__return_true' );

I keep getting the same 401 response. If I post using basic auth, it works, nonces aren't.

I'm trying to post a comment via the WP REST API. Here is my request:

axios({
    method: 'post',
    url: `${WP_API_ENDPOINT}/comments`,
    headers: {'X-WP-Nonce': wpRestSettings.nonce},
    data: {
        "post": 8911,
        "author_name": "Mr. Author",
        "author_email": "[email protected]",
        "content": "A new comment."
    }
})

I've also tried adding the nonce in the data instead under the value _wpnonce per the spec.

I saw one answer suggest adding this filter: add_filter( 'filter_rest_allow_anonymous_comments', '__return_true' );

I keep getting the same 401 response. If I post using basic auth, it works, nonces aren't.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 12, 2017 at 19:43 jackreichertjackreichert 4134 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I solved it.

add_filter( 'filter_rest_allow_anonymous_comments', '__return_true' );

should be:

add_filter( 'rest_allow_anonymous_comments', '__return_true' );

I found it by looking for the filter itself and saw that it was different than in the original answer.

本文标签: Trouble Commenting via the WP REST API using nonces