So i'm trying to make a follow button in wp, and for some reason the ajax button isn't working right. Here are the steps of what's supposed to happen

  1. user clicks #followbtn
  2. Ajax goes to $_POST action that = follow
  3. php runs wp_set_object_terms( $user_id, $author_id, 'follow', true );
  4. when that's done the function echo's "ok"
  5. if the data = ok reload the page

For some reason the php isn't executing and the page isn't being reloaded.

add_action( 'wp_ajax_nopriv_jk-author-follow', 'jk_author_follow' );
add_action( 'wp_ajax_jk-author-follow', 'jk_author_follow' );
function jk_author_follow() {
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
    die ( 'Nope!' );

if($_POST['action'] == "follow") {

$author_id = get_the_author_meta( 'nickname' ); // get authors name    
$termId = get_term_by( 'slug', $author_id, 'follow' ); // get the term id from author
$termId = $termId->term_id;
$followers = get_objects_in_term( $termId, 'follow' ); // count followers in author term
$author_follow_count = count( $followers );

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"; 

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 );
$output = '<a href="#" id="followbtn">Folllow&nbsp;'.$count.'</a>';
return $output;


$('#followbtn').on('click', function(e){

  url: ajax_var.url,
  type: 'post',
  data: {'action': 'follow'},
  success: function(data, status) {
    if(data == "ok") {
  error: function(xhr, desc, err) {
    console.log("Details: " + desc + "\nError:" + err);
   }); // end ajax call
  • 1
    Do you see any errors in your console? Do you at least see the "Nope!" message? Have you tried doing console.log(data) in your success callback to see if anything is being returned? Commented Apr 15, 2015 at 20:58

1 Answer 1


As for your PHP not being executed - the action parameter in your JavaScript AJAX call needs to match the hook name in your add_action call. Take a look at the example below. I haven't tested it but it should work. I have simplifed the AJAX call, feel free to modify it to suit your use case.

var data = {
  action: 'jk-author-follow', // Look Ma' I talk to your PHP! :)
  // ...
  // here you can add any other custom vars you need to pass to the PHP handler
$.post(ajax_var.url, data, function(response){
  if( typeof(console) == 'object' ) console.log( response );

Another thing to keep in mind is a context in which the PHP function is executed. It runs as a separate request and therefore it's not aware of the current loop etc. You might need to pass additional variables (i.e. user_id) in your data object.

As a side note - the nopriv variant of the hook runs for the users who are not logged in. If you intend your functionality exclusively for the logged-in users you might as well drop it.

There's a lot of useful info in the Highest Voted 'ajax' Questions here on WPSE. You can also take a look at my older answer to a related question: Adding callback function for wp_ajax_ has no effect. Most of the information mentioned there will be relevant for you as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.