0

I will try to be clear, I'm using "Users Following System" plugin. And I added a list of the following/followers users into the author.php profile, now I'm trying to use Ajax pagination.

For sure into author.php I used this line to get the users information.

<?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>

And this line to get the user meta of _pwuf_following

$include = get_user_meta($curauth->ID, '_pwuf_following', true);

UPDATE 1

Ajax Pagination now working but I still have the same problem, when i click load more I get the following users of the administrator!! It should bring out the following users of each author :(

This is what i did in author.php to get the list of following users as @user141080 did.

<div id="following">

    <div class="following-cont">
<?php

$include = get_user_meta($curauth->ID, '_pwuf_following', true);

if ( empty( $include ) ) {

     echo 'Not followed anyone yet.';

    } else {

$args = array (
    'order' => 'DESC',
    'include'  => $include,
    'number'  => '6',
    'paged' => 1
);

$wp_user_query = new WP_User_Query( $args );

$users = $wp_user_query->get_results();

    echo '<div id="top-artists-contributors-3">';
    echo '<ul id="grid-contributors-4">';
    echo '<li class="scroll-artists">';
    foreach ( $users as $user ) {
        $avatar_size = 90;
        $avatar = get_avatar($user->user_email, 200);
        $author_profile_url = get_author_posts_url($user->ID);
        $profile = get_userdata($user->ID);

    echo '<div class="single-item-3">';
    echo '<div class="author-gravatar-3"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
    echo '<div class="members-name"><a href="', $author_profile_url, '">' . $profile->first_name .'</a></div>';
    echo '</div>';           
    }
    echo '</li>';
    echo '</ul>';
    echo '</div>';

}
?>

</div><!-- #following -->

<div class="loadmore">More</div>

<?php
// The variables tmp_author_name and tmp_author serve only as temporary storage.
// They controls how the data should be determined on the server.
$tmp_author_name = '';
$tmp_author = 0;
if( isset($_GET['author_name']) )
{
    $tmp_author_name = $author_name;
}
else
{
    $tmp_author = intval($author);  
}
?>

<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
    $('body').on('click', '.loadmore', function() {
            var data = 
            {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author_name': '<?php echo esc_html($tmp_author_name); ?>',
                'author': '<?php echo esc_html($tmp_author); ?>'

            };

        $.post(ajaxurl, data, function(response) {

        $('.following-cont').append(response);
        page++;

        });
    });
});
</script>

        </div>

function.php of the Ajax action.

add_action('wp_ajax_user_following_by_ajax', 'user_following_by_ajax_callback');
add_action('wp_ajax_nopriv_user_following_by_ajax', 'user_following_by_ajax_callback');

function user_following_by_ajax_callback() {

    check_ajax_referer('user_more_following', 'security');
    $paged = $_POST['page'];

    $curauth = NULL;

    if( isset($_POST['author_name']) AND trim($_POST['author_name']) != '' )
    {
        $curauth = get_user_by('slug', trim($_POST['author_name']) );
    }
    elseif( isset($_POST['author']) AND intval($_POST['author']) > 0 )
    {
        $curauth = get_userdata(intval($_POST['author']));   
    }

    if( !is_null($curauth) ) {

$include = get_user_meta($curauth->ID, '_pwuf_following', true);

if ( empty( $include ) ) {

     echo 'Not followed anyone yet.';

    } else {

$args = array (
    'order' => 'DESC',
    'include'  => $include,
    'number'  => 6,
    'paged' => $paged
);

$wp_user_query = new WP_User_Query( $args );

$users = $wp_user_query->get_results();

    echo '<div id="top-artists-contributors-3">';
    echo '<ul id="grid-contributors-4">';
    echo '<li class="scroll-artists">';
    foreach ( $users as $user ) {
        $avatar_size = 90;
        $avatar = get_avatar($user->user_email, 200);
        $author_profile_url = get_author_posts_url($user->ID);
        $profile = get_userdata($user->ID);

    echo '<div class="single-item-3">';
    echo '<div class="author-gravatar-3"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
    echo '<div class="members-name"><a href="', $author_profile_url, '">' . $profile->first_name .'</a></div>';
    echo '</div>';           
    }
    echo '</li>';
    echo '</ul>';
    echo '</div>';
    }
    wp_die();
    }
}

So now how can i get the following users of each author, And why i got the administrator following users instead of their users?

0

The problem is this line:

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); 

First you make the ajax call with an POST-request, that is the reason why the GET array is empty. Second the variable $author is not defined. So every time you call the script, the variable $curauth is empty.

A solution could be that you send the author name with the ajax call like the page or the nonce.


EDIT: In the file author.php you detect the author data ($curauth) with the name (get_user_by('slug', $author_name)) or with the author id (get_userdata(intval($author))). To do the same on the server (function.php) you have to the send the same data via the ajax call.

<?php
// The variables tmp_author_name and tmp_author serve only as temporary storage.
// They controls how the data should be determined on the server.
$tmp_author_name = '';
$tmp_author = 0;
if( isset($_GET['author_name']) )
{
    $tmp_author_name = $author_name;
}
else
{
    $tmp_author = intval($author);  
}
?>

<script type="text/javascript">

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
var canBeLoaded = true,
bottomOffset = 2000;

jQuery(function($) 
{

    $(window).scroll(function() 
    {

        if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ) 
        {

            canBeLoaded = false;
            /// add tmp_author_name and tmp_author to the ajax data array 
            var data = 
            {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>'
                'author_name': <?php echo esc_html($tmp_author_name); ?>,
                'author': <?php echo esc_html($tmp_author); ?>,

            };

            $.post(ajaxurl, data, function(response) 
            {
                $('#following').append(response);
                canBeLoaded = true;
                page++;
            });
        }
    });
});
</script>

On the server you read the post data and then detect the author.

add_action('wp_ajax_user_following_by_ajax', 'user_following_by_ajax_callback');
add_action('wp_ajax_nopriv_user_following_by_ajax', 'user_following_by_ajax_callback');

function user_following_by_ajax_callback() 
{
    check_ajax_referer('user_more_following', 'security');
    $paged = $_POST['page'];

    /// detect the author data with the name or with the author id
    $curauth = NULL;

    if( isset($_POST['author_name']) AND trim($_POST['author_name']) != '' )
    {
        $curauth = get_user_by('slug', trim($_POST['author_name']) );
    }
    elseif( isset($_POST['author']) AND intval($_POST['author']) > 0 )
    {
        $curauth = get_userdata( intval($_POST['author']) );    
    }

    if( !is_null($curauth) )
    {
        $include = get_user_meta($curauth->ID, '_pwuf_following', true);


        if ( !empty( $include ) ) 
        {

            echo 'Not followed anyone yet.';

        } 
        else 
        {
            //// continue with your existing code. /////
        }

    }
    else
    {
        echo 'Not followed anyone yet.';
    }
}
11
  • Thanks so much for your answer @user141080, Can you please show me example, because i still newbie with wp. – Adham Mohamed Oct 4 '18 at 5:55
  • @AdhamMohamed I edited my answer...maybe it is helpful for you – user141080 Oct 5 '18 at 10:58
  • Thanks bro, but the function code gives me error! – Adham Mohamed Oct 5 '18 at 19:53
  • Maybe in this line $curauth = get_userdata(intval(intval($_POST['author'])); there are one more intval or I'm wrong? anyways after i removed one intval it working now but when I visit any of our authors profile I still get in the next page my own following users not his following users, I mean the next page showing main users and i need to show the current following users for each author, did you get it now? – Adham Mohamed Oct 5 '18 at 20:51
  • Yes the second intval was wrong, it was an copy and past error. Sry. Yes, I understand your problem, I suspect it has something to do with $author_name or $author. In your posted code from the file author.php it is not clear where these two get their values ​​from. – user141080 Oct 6 '18 at 13:21

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.