I’m working on my first plugin. I would like to add a follow button for the posts, so the logged in users can follow the authors.

I placed the following lines into functions.php

function add_query_vars_filter( $vars ){
       $vars[] = "userID";
       return $vars;
  }
add_filter( 'query_vars', 'add_query_vars_filter' );
// The code to display the button
function show_follow_button() {
    $author_ID = get_the_author_meta('ID');
    $current_user = wp_get_current_user();
    if ( is_user_logged_in() && $current_user->ID != $author_ID ) {
        return '<a href="#'.$author_ID.'" title="" class="follow-user">Follow</a>';
    }
}
// The update function, I know I have to use array for the ids, it's for only test
$userID = (get_query_var('userID')) ? get_query_var('userID') : false;
if(is_numeric($userID)) {
    $current_user = wp_get_current_user();
    update_user_meta($current_user->ID, 'following', $_GET['userID']);
}
// jQuery
$('.follow-user').click(function(e) {
    e.preventDefault();
    var userID = $(this).attr('href').slice(1);
    $.ajax({
            type: "GET",
            url: "index.php",
            data: 'userID='+userID,
            success: function(msg){
                $('.follow-user').html('Followed');
            }
        });
});

After I click to the follow button I see on the net tab: index.php?userID=X, but nothing happens in the database. Where is the problem?

Read more here: How to work with AJAX and WordPress?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, ,

Wordpress related questions and answers: