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.';
}
}