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:

 $(".blavatar").click(function(){

        //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

        jQuery.ajax({
                type:"POST",
                url: ajaxurl + "?action=fdiAddFavorite",
                data: {
                        action: 'fdiAddFavorite',
                        fav_id: fav_id,
                },
                success:function(data){
                $('#wp-admin-bar-my-favorite-list').append($(fav.html()));
                console.log(data);
                //console.log(fav_id);

                },
                error: function(errorThrown){
                        alert(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);
    //print_r($favorite_list);
    if(empty($favorite_list))
        $favorite_list = array();

    $favorite_list[] = $fav_id;
    update_user_meta($user_id, 'favorite links', $favorite_list);
    //sort($favorite_list);
    //print_r($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.
Thanks

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 *