I am trying to figure out how to send a post request to a WP Rest API function on an AMP Page.

The endpoint requires 2 fields, post_id & nonce.

/wp-json/wordpress-popular-posts/v2/views/[post_id] as per the guide on https://github.com/cabrerahector/wordpress-popular-posts/wiki/8.-REST-API-Endpoints

Now the task here is how to send a request from the AMP page to this endpoint. My theme offers the ability to add custom html/javascript/CSS code to the AMP page.

But how can we get the nonce and post_id dynamically using Javascript?

Can the below code help me solve the issue?

    url: foo,
    method: "POST",
    beforeSend: function(xhr) {
        xhr.setRequestHeader("X-WP-Nonce", wpApiSettings.nonce);
    data: {
        "foo": bar
}).done(function(response) {
    // your custom code

1 Answer 1


You need to localize the nonce here's how: https://developer.wordpress.org/reference/functions/wp_localize_script/

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.