1

I just can't seem to get my jquery to call my php handler function in my plugin. Please help. TQ

UPDATED : I updated the codes. It is working now.

My php file

// INIT
function nizam_scripts() {
    wp_enqueue_script( 'nizam_ajax', plugin_dir_url( __FILE__ ) . '/nizam_plugin.js', array( 'jquery' ) );
    wp_localize_script('nizam_ajax', 'nizam_handler', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );    
}
add_action('wp_print_scripts', 'nizam_scripts'); 


// SHORTCODE
function nizam_shortcode(){
    $html .= '<input size="10" type="text" class="ajax_bahagian" name="bahagian" value="100">';
    $html .= '<button class="btn_add" onclick="window.location.href = \'javascript:void(0)\';">Add</button>';                                  
    return $html;       
}      
add_shortcode( 'nizam_plugin', 'nizam_shortcode' );

// HANDLER     
function nizam_handler() {
    if ( isset($_REQUEST) ) {
        $bahagian  = $_REQUEST['bahagian']; 
        echo $bahagian;
    }  

    // Always die in functions echoing ajax content
    wp_die();
}

add_action( 'wp_ajax_nizam_handler', 'nizam_handler' );
add_action( 'wp_ajax_nopriv_nizam_handler', 'nizam_handler' );

My jQuery

    jQuery(document).ready( function($) {

    $('.btn_add').on('click', function(event) {
        var bahagian = $('.ajax_bahagian').val();

        $.ajax({
            url: ajaxurl, 
            data: {
                'action'    : 'nizam_handler', 
                'bahagian'  : bahagian
            },
            success:function(data) {
                // This outputs the result of the ajax request
                console.log(data);
            },
            error: function(errorThrown){ 
                console.log(errorThrown);
            }
        });      

    });          


}); 
0

For logged in users, use:

  • wp_ajax_{$action}

For logged out users, use:

  • wp_ajax_nopriv_{$action}

Your code is currently using wp_ajax_{$action} -> wp_ajax_nizam_handler which will restrict the request to logged in (authenticated users).

If you are logged out at the time of the request, you will never make it into your callback.

If you want to target logged out users add an action for the wp_ajax_nopriv_{$action} context.

Example:

function wpse_356672_nizam_handler_handler() {

    wp_send_json_success( [
        'results'  => 'MY_VALUE',
        'custom_property'  => 'ANOTHER_VALUE',
        // etc...
    ] );

}

add_action('wp_ajax_nizam_handler', 'wpse_356672_nizam_handler_handler' );
add_action('wp_ajax_nopriv_nizam_handler', 'wpse_356672_nizam_handler_handler' );

NOTE: you can use both hooks simultaneously OR one or the other, depending on your use case.

TIP: instead of using die it is recommended that you use wp_die but even more so wp_send_json_success, wp_send_json_error or wp_send_json.

Recommended Reading:

Aditional Reading (security):

| improve this answer | |
  • Noted on the wp_ajax_nopriv_{$action} But my main issue is call from the jquery is not calling the php handler. Value from jquery is not passed to php. I might have missed something. – Enaizi Jan 18 at 9:58
  • Do you end up in the nizam_handler function? If so, var_dump( $_REQUEST );. If not, are you sure you are logged in and have you tried the nopriv context simultaneously? What does console.log(response); say and do you see any errors or notices in Dev Tools? – Adam Jan 18 at 10:00
  • Got an error HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax. (XHR)POST - pewarisan.com/wp-admin/admin-ajax.php – Enaizi Jan 18 at 10:27
  • Yes, already add nopriv – Enaizi Jan 18 at 10:32
  • 1
    OK, got it to work. There were some typos. – Enaizi Jan 18 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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