0

I know its full of questions about this on SO, and I have read most and applied all possible solutions, thus its still not working.

I have some ajax functions already up and working but for some reason this last one is not working.

I have simple ajax request which sends header to admin-ajax.php:

status is 200 (ok)

and header is:

action:linked_post_image_grid

the code in my php file is really basic for testing:

add_action('wp_ajax_nopriv_linked_post_image_grid', 'linked_post_image_grid');
add_action('wp_ajax_linked_post_image_grid', 'linked_post_image_grid');

function linked_post_image_grid(){

    echo 'this is return message';

    die();
}

now for some reason this only and always returns 0

2
  • Have you considered using a simple REST API endpoint instead?
    – Tom J Nowell
    Jun 22, 2017 at 11:13
  • Also if the others are working and this one is not, there should be some difference comparing to the others. And the above code is not exactly enough to debug something like this. When you say action:linked_post_image_grid I hope you mean action:'linked_post_image_grid' edit: just realized I might not be understanding how your setup is... so ignore my ignorance on the action :)
    – jimihenrik
    Jun 22, 2017 at 11:37

1 Answer 1

3

There's a simple alternative that sidesteps this issue entirely with a modern and easy to use interface, just use a REST API endpoint!

So lets turn this:

add_action('wp_ajax_nopriv_linked_post_image_grid', 'linked_post_image_grid');
add_action('wp_ajax_linked_post_image_grid', 'linked_post_image_grid');

function linked_post_image_grid(){

    echo 'this is return message';

    die();
}

Into this:

add_action( 'rest_api_init', function () {
        register_rest_route( 'buxbeatz/v1', '/linked_post_image_grid/', array(
                'methods' => 'GET',
                'callback' => 'linked_post_image_grid'
        ) );
} );

function linked_post_image_grid( $request ) {
    return "this is return message";
}

Now you can visit example.com/wp-json/buxbeatz/v1/linked_post_image_grid and you'll get a JSON response of "this is return image"

6
  • didnt think about REST api. Wil ltry to use it form now on. I actually get rest_no_route with your example
    – user106021
    Jun 22, 2017 at 12:29
  • make sure you have rewrite rules turned on and that the code is somewhere it gets ran/loaded, aka in a plugin or functions.php, not inside a template
    – Tom J Nowell
    Jun 22, 2017 at 13:10
  • the rest api iteslf works going to localhost/sitename/wp-json works... the scripts are loaded in functions.php. Its the custom endpoints created with the function that return 404
    – user106021
    Jun 22, 2017 at 13:20
  • I can only suggest clearing caches and flushing permalinks, then checking that what you visit /wp-json that it lists the new endpoint in there
    – Tom J Nowell
    Jun 22, 2017 at 13:32
  • It's based off of an article I wrote here which works fine on my site ( you can visit the endpoint in the article )
    – Tom J Nowell
    Jun 22, 2017 at 13:33

Your Answer

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