Powered By Blogger

Tuesday, March 28, 2017

call wordpress function in jquery ajax

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29


jQuery.post(
    ajaxurl, 
    {
        'action': 'add_foobar',
        'data':   'foobarid'
    }, 
    function(response){
        alert('The server responded: ' + response);
    }
);
function.php
add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );

function prefix_ajax_add_foobar() {
    // Handle request then generate response using WP_Ajax_Response

    // Don't forget to stop execution afterward.
    wp_die();
}

No comments:

Post a Comment