WordPress function is not called and ajax return 0

I am developing a plugin called my-search-site and in that I have site links. If I click on the link, the jQuery function is called and that jQuery variable is passed to wordpress function from which It can be added to user meta.

My jQuery looks like this:


        //toggle between the star icon 
        $(this).toggleClass('star-empty star_filled');
        //retrieve the clicked link's ID and it's href
        var fav_id =  $(this).parent().parent().attr('id');
        fav = $(this).parent().parent();
        //append favorites using ajax call

                url: ajaxurl + "?action=fdiAddFavorite",
                data: {
                        action: 'fdiAddFavorite',
                        fav_id: fav_id,

                error: function(errorThrown){

        //}).always(function() { alert("complete");

        return false;

and on the same page my wordpress function is declared as below:

 add_action('wp_ajax_fdiAddFavorite', 'fdiAddFavorite');
 add_action('wp_ajax_nopriv_fdiAddFavorite', 'fdiAddFavorite');

 function fdiAddFavorite() {
    $fav_id = get_post($_REQUEST['fav_id']);
    $user_id = get_current_user_id();
    $favorite_list = get_user_meta($user_id, 'favorite links', true);
        $favorite_list = array();

    $favorite_list[] = $fav_id;
    update_user_meta($user_id, 'favorite links', $favorite_list);
    die('{"success": true}');

After click event the ajax returns 0 and the function is not called and can not be recognized by ajax call. I am not able to update the user meta. Can you please help me with it.

Read more here: WordPress function is not called and ajax return 0

Leave a Reply

Your email address will not be published. Required fields are marked *