I've defined a PHP callback function to handle the communication between server-side and client-side:

function json_render_modal_body() {

    check_ajax_referer( THEME_PREFIX . '-modals', 'nonce' );

    $response = array( 'text' => 'no' );
    $modal_id = null;

    if ( isset( $_GET['modal_id'] ) && ! $modal_id = filter_var( $_GET['modal_id'], FILTER_SANITIZE_STRING ) ) {
        wp_send_json_error( $response );

    if ( ! $form = render_form( $modal_id ) ) {
        wp_send_json_error( $response );

    $response['text'] = 'yes';

    wp_send_json_success( $response );


I've told WordPress about this function, to handle the communication process:

add_action( 'wp_ajax_json_render_modal_body', __NAMESPACE__ . '\json_render_modal_body' );
add_action( 'wp_ajax_nopriv_json_render_modal_body', __NAMESPACE__ . '\json_render_modal_body' );

I've registered/enqueued/localized my JS script to handle the AJAX request.

This is what I've localized:

    THEME_PREFIX . '-modals',
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        'nonce' => wp_create_nonce( THEME_PREFIX . '-modals' )

This is what my JS script looks like:

var modal_id = jQuery( e.relatedTarget ).attr( "data-target" ).replace( "#", "" );

        action: "json_render_modal_body",
        modal_id: modal_id,
        nonce: mbe_theme_modal.nonce
    function ( data ) {

        console.log( data );


Any idea why I keep getting a 0 in my AJAX response?

I've also tried removing the nonce stuff from my code, and accessing the direct URL (domain.com/wp-admin/admin-ajax.php?action=json_render_modal_body&modal_id=some-modal-id, however, I still continue to get a 0.

I've even tried keeping the PHP function super simple with nothing but a simple text response, and still continue to get a 0.

  • 4
    Have you considered using the REST API instead of WP AJAX?
    – Tom J Nowell
    Jun 12, 2017 at 21:26
  • 1
    Seeing just 0 typically means there is no function currently hooked to that action. Make sure your add_action calls are getting executed on ajax requests.
    – Milo
    Jun 12, 2017 at 23:15
  • 1
    a guess, the namspace is not right Jun 13, 2017 at 3:43
  • @TomJNowell I'd like to use the REST API if it'll get the job done. Could you show me a RESTful example as an answer to this question, or should I ask a new question? Jun 14, 2017 at 15:43

1 Answer 1


Usually, you can use the global variable ajaxurl instead of any path using admin-ajax.php.

More importantly, the PHP function should echo the response before calling wp_die(). Because you haven't called wp_die(), AJAX is probably waiting for more from PHP.

Hope this helps.

P.S. What is that 'json' string doing where the AJAX fail() function should be?

  • Since he's using wp_send_json_success() he doesn't need to add a die() call; that's handled by wp_send_json() automatically. Jun 13, 2017 at 1:06
  • 3
    iajaxurl is defined only on "admin". In any case, if it was wrong there would be no response at all. Jun 13, 2017 at 3:47

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.