3

I'm trying to make a user follow button using AJAX, but for some reason my AJAX isn't talking to my jk_user_follow function and setting the wp_set_object_terms.

PHP

function register_user_taxonomy(){
    register_taxonomy( 'follow' , 'user' , $args );
}
add_action( 'init', 'register_user_taxonomy' );

/**
* (1) Enqueue scripts for follow system
*/
function follow_scripts() { 
    wp_enqueue_script( 'jk_follow_user', get_template_directory_uri().'/js/follow.js',                     array('jquery'), '1.0', 1 );
    wp_localize_script( 'jk_follow_user', 'ajax_var', array(
        'url'   => admin_url( 'admin-ajax.php' ),
        'nonce' => wp_create_nonce( 'ajax-nonce' )
    ) );
}
add_action( 'init', 'follow_scripts' );

/**
* (2) follow the user
*/
add_action( 'wp_ajax_jk-user-follow', 'jk_user_follow' );
function jk_user_follow() {

    $nonce = $_POST['nonce'];
    if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
        die ( 'Nope!' );

    if ( isset( $_POST['jk_user_follow'] ) ) {

        $author_id = get_the_author_meta( 'nickname' ); // get authors name

        if ( is_user_logged_in() ) { // user is logged in
            $user_id = get_current_user_id(); // current user
            wp_set_object_terms( $user_id, $author_id, 'follow', true ); // Follow the author 
            echo "ok";  
        } else {
            echo "login";
        }

    }
    exit;
}

Front-End Button

function getAuthorFollowLink( $author_id ) {
    $author     = get_the_author_meta( 'nickname' );
    $user_ID    = get_current_user_id();
    $termId     = get_term_by( 'slug', $author, 'follow' ); // get the term id from author
    $termId     = $termId->term_id;
    $followers  = get_objects_in_term( $termId, 'follow' ); // count followers in author term
    $count      = count( $followers );

    if ( is_object_in_term( $user_ID, 'follow', $author ) ) {
        $class = esc_attr( ' following' );
        $title = esc_attr( 'Unfollow' );
        $heart = 'Following';
    } else {
        $class = esc_attr( '' );
        $title = esc_attr( 'Like' );
        $heart = 'Follow';
    }

    $output = '<a href="#" class="jk-user-follow'.$class.'" data-author="'.$author.'" title="'.$title.'">'.$heart.'&nbsp;'.$count.'</a>';
    return $output;
}

JQuery Update

$(function(){
$('.jk-user-follow').on('click', function(e){
e.preventDefault();
$('.jk-user-follow').fadeOut(300);
$post = jQuery(this);
author = $post.data('author');
$.ajax({
  type: "post",
  url: ajax_var.url,
  data : "action=jk-user-follow&nonce="+ajax_var.nonce+"&jk_user_follow=&author"+author,
  success: function(response) {
    // You can put any code here to run if the response is successful.

    // This will allow you to see the response
    console.log(response);
  }
 });
});
  • Have you enabled debugging to ensure there's no syntax errors? – Howdy_McGee Apr 28 '15 at 19:17
  • There are some errors, but they don't have anything to do with the ajax of this script – Julian Apr 28 '15 at 19:25
  • But it does say Nope! in the javascript console, which means there's a problem with the nonce – Julian Apr 28 '15 at 19:29
  • What if you change your data to { author: $post.data('author'), nonce: ajax_var.nonce }? – czerspalace Apr 28 '15 at 19:52
  • Okay I figured out the problem... The php is being executed, but it can't find the current authors name in author_id = get_the_author_meta( 'nickname' ); // get authors name Not sure how to get it though – Julian Apr 28 '15 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.