好吧,这次我会尽量说清楚的,我真的需要你们的帮助。
我使用一个与Ajax一起工作的插件来创建一个“跟随”按钮,让我们的用户互相跟踪。
我把下面的按钮放在我需要一个用户跟踪其他用户的任何地方,就像那个echo pwuf_get_follow_unfollow_links( //user id here );
我面临的问题是,如果我在任何Ajax分页模板中添加了这个回显函数--无论是在Ajax分页后还是用户列出Ajax分页,下面的按钮都不能工作,并且在单击时找不到用户id!
但是可以肯定的是,在我将更多信息加载到第二个页面之前,他可以在第一个页面上找到用户id!
例句:如果我有一个类似于这个Ajax的分页,并且试图在这个ajax分页中回显下面的按钮函数,那么这个页面就不能工作了!?
add_action('wp_ajax_loadmore_by_ajax', 'loadmore_by_ajax_callback');
add_action('wp_ajax_nopriv_loadmore_by_ajax', 'loadmore_by_ajax_callback');
function loadmore_by_ajax_callback() {
//do something
//echoing follow button
echo pwuf_get_follow_unfollow_links( //user id here );
}
我将在下面添加我的所有插件代码,我希望得到一些帮助来解决这个问题,谢谢您的时间。
follow.js
jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( '.follow-links a' ).on('click', function(e) {
e.preventDefault();
var $this = $(this);
if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
alert( pwuf_vars.login_required );
return;
}
var data = {
action: $this.hasClass('follow') ? 'follow' : 'unfollow',
user_id: $this.data('user-id'),
follow_id: $this.data('follow-id'),
nonce: pwuf_vars.nonce
};
$this.closest('.follow-links').find('img.pwuf-ajax').show();
$.post( pwuf_vars.ajaxurl, data, function ( response ) {
if ( response == 'success' ) {
if ( $this.hasClass( 'follow' ) ) {;
$this.removeClass( 'follow' ).addClass( 'unfollow' );
$this.find( 'span' ).text( 'Unfollow' );
} else {;
$this.removeClass( 'unfollow' ).addClass( 'follow' );
$this.find( 'span' ).text( 'Follow' );
}
} else {
alert( pwuf_vars.processing_error );
}
$this.closest('.follow-links').find('img.pwuf-ajax').hide();
});
});
});
actions.php
display-functions.php
follow-functions.php
$follow ) {
if ( $follow == $unfollow_user ) {
unset( $following[$key] );
$modified = true;
}
}
if ( $modified ) {
if ( update_user_meta( $user_id, '_pwuf_following', $following ) ) {
pwuf_decrease_followed_by_count( $unfollow_user );
}
}
}
// get all IDs that follow the user we have just unfollowed so that we can remove $user_id
$followers = pwuf_get_followers( $unfollow_user );
if ( is_array( $followers ) && in_array( $user_id, $followers ) ) {
$modified = false;
foreach ( $followers as $key => $follower ) {
if ( $follower == $user_id ) {
unset( $followers[$key] );
$modified = true;
}
}
if ( $modified ) {
update_user_meta( $unfollow_user, '_pwuf_followers', $followers );
}
}
if ( $modified ) {
do_action( 'pwuf_post_unfollow_user', $user_id, $unfollow_user );
return true;
}
return false;
}
/**
* Retrieve following count
*
* Gets the total number of users that the specified user is following
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to retrieve a count for
* @return int
* @param int|string|WP_User $user_id User ID or object.
* @return bool Whether the user exists
*/
function pwuf_get_following_count( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = pwuf_get_following( $user_id );
$count = 0;
if ( $following ) {
$count = count( $following );
}
return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}
/**
* Retrieve follower count
*
* Gets the total number of users that are following the specified user
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to retrieve a count for
* @return int
*/
function pwuf_get_follower_count( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$followed_count = get_user_meta( $user_id, '_pwuf_followed_by_count', true );
$count = 0;
if ( $followed_count ) {
$count = $followed_count;
}
return (int) apply_filters( 'pwuf_get_follower_count', $count, $user_id );
}
/**
* Increase follower count
*
* Increments the total count for how many users a specified user is followed by
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to increease the count for
* @return int
*/
function pwuf_increase_followed_by_count( $user_id = 0 ) {
do_action( 'pwuf_pre_increase_followed_count', $user_id );
$followed_count = pwuf_get_follower_count( $user_id );
if ( $followed_count !== false ) {
$new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );
} else {
$new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );
}
do_action( 'pwuf_post_increase_followed_count', $user_id );
return $new_followed_count;
}
/**
* Decrease follower count
*
* Decrements the total count for how many users a specified user is followed by
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to decrease the count for
* @return int
*/
function pwuf_decrease_followed_by_count( $user_id ) {
do_action( 'pwuf_pre_decrease_followed_count', $user_id );
$followed_count = pwuf_get_follower_count( $user_id );
if ( $followed_count ) {
$count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );
do_action( 'pwuf_post_increase_followed_count', $user_id );
}
return $count;
}
/**
* Check if a user is following another
*
* Increments the total count for how many users a specified user is followed by
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user doing the following
* @param int $followed_user - the ID of the user to check if being followed by $user_id
* @return int
*/
function pwuf_is_following( $user_id, $followed_user ) {
$following = pwuf_get_following( $user_id );
$ret = false; // is not following by default
if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
$ret = true; // is following
}
return $ret;
}
scripts.php
__( 'There was a problem processing your request.', 'pwuf' ),
'login_required' => __( 'Oops, you must be logged-in to follow users.', 'pwuf' ),
'logged_in' => is_user_logged_in() ? 'true' : 'false',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'follow_nonce' )
) );
}
add_action( 'wp_enqueue_scripts', 'pwuf_load_scripts' );
发布于 2019-02-22 20:04:10
我认为,如果您更改您的jQuery单击操作选择器来处理事件委托,那么下面的按钮在ajax添加之后应该可以工作。
// Change this
$( '.follow-links a' ).on('click', function(e) {
// to this
$( '.some-parent-not-added-by-ajax' ).on('click', '.follow-links a', function(e) {
// or as last resort
$( document ).on('click', '.follow-links a', function(e) {
https://wordpress.stackexchange.com/questions/329632
复制相似问题