Skip to content Skip to sidebar Skip to footer

How To Handle Ajax Post Requests In A Wordpress Plugin?

I'm trying to figure out how Ajax and Wordpress plugins work, but it seems to be pretty badly documented. Ideally what I want is: The plugin gets it own Ajax url to use, this is d

Solution 1:

I implemented below own AJAX functionality in functions.php, Just try this in plugin,

functions.php :

functionprefix_ajax_delete_search() {
    global$wpdb;
    $user_id = get_current_user_id();
    $wpdb->delete('sf_save_search', array('id' => $_POST['tid']) ); 
    wp_die();
}
add_action( 'wp_ajax_delete_search', 'prefix_ajax_delete_search' );

Script.js:

functiondeleteSearch( tid) {
    var url = window.location.href;

    jQuery.post(
        ajaxurl, 
        {
            'action': 'delete_search',
            'tid':   tid,
        },
        function(response){
          location.reload();
        }
    );
}

Post a Comment for "How To Handle Ajax Post Requests In A Wordpress Plugin?"