Skip to main content
added 296 characters in body
Source Link

Another Problem

I manually executed the code below just to see if the count is affected, and for some reason it's not, and just stays at 1.

$user_id = 1;
$author_id = 118;
wp_set_object_terms( $user_id, $author_id, 'wpse_180398_followers', true );

Another Problem

I manually executed the code below just to see if the count is affected, and for some reason it's not, and just stays at 1.

$user_id = 1;
$author_id = 118;
wp_set_object_terms( $user_id, $author_id, 'wpse_180398_followers', true );
added 30 characters in body
Source Link

Okay I fiddled with (4) and used your code, am I on. For some reason the right track?default follower count is still 1. Also where do I place the die for the ajax? Thanks

Okay I fiddled with (4) and used your code, am I on the right track? Also where do I place the die for the ajax? Thanks

Okay I fiddled with (4) and used your code. For some reason the default follower count is still 1. Also where do I place the die for the ajax? Thanks

added 20 characters in body
Source Link
/**
* (1) Enqueue scripts for follow system
*/
function follow_scripts() {
wp_enqueue_script( 'jk_follow_post', get_template_directory_uri().'/js/post-   like.min.js', array('jquery'), '1.0', 1 );
 wp_localize_script( 'jk_like_post', 'ajax_var', array(
    'url' => admin_url( 'admin-ajax.php' ),
    'nonce' => wp_create_nonce( 'ajax-nonce' )
        )
    );
}
/**
* (2) Save follow data
*/
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 ( isset( $_POST['jk_author_follow'] ) ) {
    
    $author_id = $_POST['author_id']; // author id
    
    
    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id(); // current user
        
        // create the term if it doesn't exist
        if ( !term_exists( $author_id, 'wpse_180398_followers' ) ) {
            wp_insert_term( $author_id, 'wpse_180398_followers' );
        }

        // follow the user, note the last argument is true
        $term_ids = wp_set_object_terms( $user_id, $author_id, 'wpse_180398_followers', true );

        if ( is_wp_error( $term_ids ) ) {
            // There was an error somewhere and the terms couldn't be set.
            echo "error";
        } else {
            // Success!
        }
    }
}
}
/**
 * (3) Test if user already liked post
 */
function AlreadyFollowed( $author_id ) { // test if user is following author
$user_id = get_current_user_id(); // current user
$author_id = $_POST['author_id']; // author id
if ( is_user_logged_in() ) { // user is logged in
    if ( has_term( $author_id, 'wpse_180398_followers', $user_id ) ) {
        // user_id is following author_id!
    }
}
}
/**
  * (4) Front end button
*/
function getFollowLink( $author_id ) {
$following = '';
$author_id = the_author_metaget_query_var($userId'author');
$user_id = get_current_user_id(); // current user
if ( has_term( $author_id, 'wpse_180398_followers', $user_id ) ) {
    $following = ' following';
}
echo '<a class="follow-button' .$following. '" data-author="' .$author_id.  '">Follow</a>';
}
/**
 * (5) Add a shortcode to your posts instead
 * type [jkfollower] in your post to output the button
*/
function jk_follow_shortcode() {
return getFollowLink( get_the_ID() );
}
add_shortcode('jkfollower', 'jk_follow_shortcode');
/**
* (1) Enqueue scripts for follow system
*/
function follow_scripts() {
wp_enqueue_script( 'jk_follow_post', get_template_directory_uri().'/js/post-   like.min.js', array('jquery'), '1.0', 1 );
 wp_localize_script( 'jk_like_post', 'ajax_var', array(
    'url' => admin_url( 'admin-ajax.php' ),
    'nonce' => wp_create_nonce( 'ajax-nonce' )
        )
    );
}
/**
* (2) Save follow data
*/
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 ( isset( $_POST['jk_author_follow'] ) ) {
    
    $author_id = $_POST['author_id']; // author id
    
    
    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id(); // current user
        
        // create the term if it doesn't exist
        if ( !term_exists( $author_id, 'wpse_180398_followers' ) ) {
            wp_insert_term( $author_id, 'wpse_180398_followers' );
        }

        // follow the user, note the last argument is true
        $term_ids = wp_set_object_terms( $user_id, $author_id, 'wpse_180398_followers', true );

        if ( is_wp_error( $term_ids ) ) {
            // There was an error somewhere and the terms couldn't be set.
            echo "error";
        } else {
            // Success!
        }
    }
}
}
/**
 * (3) Test if user already liked post
 */
function AlreadyFollowed( $author_id ) { // test if user is following author
$user_id = get_current_user_id(); // current user
$author_id = $_POST['author_id']; // author id
if ( is_user_logged_in() ) { // user is logged in
    if ( has_term( $author_id, 'wpse_180398_followers', $user_id ) ) {
        // user_id is following author_id!
    }
}
}
/**
  * (4) Front end button
*/
function getFollowLink( $author_id ) {
$author_id = the_author_meta($userId);
$user_id = get_current_user_id(); // current user
if ( has_term( $author_id, 'wpse_180398_followers', $user_id ) ) {
    $following = ' following';
}
echo '<a class="follow-button' .$following. '" data-author="' .$author_id. '">Follow</a>';
}
/**
 * (5) Add a shortcode to your posts instead
 * type [jkfollower] in your post to output the button
*/
function jk_follow_shortcode() {
return getFollowLink( get_the_ID() );
}
add_shortcode('jkfollower', 'jk_follow_shortcode');
/**
* (1) Enqueue scripts for follow system
*/
function follow_scripts() {
wp_enqueue_script( 'jk_follow_post', get_template_directory_uri().'/js/post-   like.min.js', array('jquery'), '1.0', 1 );
 wp_localize_script( 'jk_like_post', 'ajax_var', array(
    'url' => admin_url( 'admin-ajax.php' ),
    'nonce' => wp_create_nonce( 'ajax-nonce' )
        )
    );
}
/**
* (2) Save follow data
*/
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 ( isset( $_POST['jk_author_follow'] ) ) {
    
    $author_id = $_POST['author_id']; // author id
    
    
    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id(); // current user
        
        // create the term if it doesn't exist
        if ( !term_exists( $author_id, 'wpse_180398_followers' ) ) {
            wp_insert_term( $author_id, 'wpse_180398_followers' );
        }

        // follow the user, note the last argument is true
        $term_ids = wp_set_object_terms( $user_id, $author_id, 'wpse_180398_followers', true );

        if ( is_wp_error( $term_ids ) ) {
            // There was an error somewhere and the terms couldn't be set.
            echo "error";
        } else {
            // Success!
        }
    }
}
}
/**
 * (3) Test if user already liked post
 */
function AlreadyFollowed( $author_id ) { // test if user is following author
$user_id = get_current_user_id(); // current user
$author_id = $_POST['author_id']; // author id
if ( is_user_logged_in() ) { // user is logged in
    if ( has_term( $author_id, 'wpse_180398_followers', $user_id ) ) {
        // user_id is following author_id!
    }
}
}
/**
  * (4) Front end button
*/
function getFollowLink( $author_id ) {
$following = '';
$author_id = get_query_var('author');
$user_id = get_current_user_id(); // current user
if (has_term( $author_id, 'wpse_180398_followers', $user_id )) {
    $following = ' following';
}
echo '<a class="follow-button' .$following. '" data-author="' .$author_id.  '">Follow</a>';
}
/**
 * (5) Add a shortcode to your posts instead
 * type [jkfollower] in your post to output the button
*/
function jk_follow_shortcode() {
return getFollowLink( get_the_ID() );
}
add_shortcode('jkfollower', 'jk_follow_shortcode');
deleted 8 characters in body
Source Link
Loading
added 32 characters in body
Source Link
Loading
deleted 100 characters in body
Source Link
Loading
added 17 characters in body
Source Link
Loading
added 32 characters in body
Source Link
Loading
deleted 1594 characters in body
Source Link
Loading
added 4576 characters in body
Source Link
Loading
Source Link
Loading