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

4
  • 1
    What do you mean by your Ajax function being "on the same page"? What file do you declare that in? Remember that Ajax requests are a separate http request from the page you are making the requests from. – Milo Apr 5 '17 at 21:58
  • the ajax query and the function which is called by ajax are on the same page. I haven't make two different files for that. – patel jinal Apr 5 '17 at 23:11
  • How are you adding the javascript to your file? – czerspalace Apr 5 '17 at 23:48
  • Your Ajax function won't be loaded on the Ajax request because that is a different page- your Ajax function should be in a file that is loaded on all requests. – Milo Apr 6 '17 at 11:55
0

Instead of url: ajaxurl + "?action=fdiAddFavorite",, use two lines like this:

url: ajaxurl,
action: "fdiAddFavorite",

Then at the end of fdiAddFavorite(), you try something like this:

echo $favorite_list; // could also send message like "success!"
wp_die();

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.