Javascript 通过Ajax更新用户元数据

Javascript 通过Ajax更新用户元数据,javascript,php,ajax,wordpress,meta,Javascript,Php,Ajax,Wordpress,Meta,有人能告诉我我做错了什么吗?我有一个ajax函数,每隔几秒钟运行一次,它基本上检查两个数据库表,如果它们不匹配,则更新其中一个数据库表以匹配另一个数据库表,但出于某种原因,它不会更新数据库表。我如何编辑函数以便实现这一点 以下是所涉及的功能: function tb_update_old_followers($user_id) { $followers = tb_get_follower_count($user_id); $old_followers = tb_get_old

有人能告诉我我做错了什么吗?我有一个ajax函数,每隔几秒钟运行一次,它基本上检查两个数据库表,如果它们不匹配,则更新其中一个数据库表以匹配另一个数据库表,但出于某种原因,它不会更新数据库表。我如何编辑函数以便实现这一点

以下是所涉及的功能:

function tb_update_old_followers($user_id) {

    $followers = tb_get_follower_count($user_id);

    $old_followers = tb_get_old_follower_count($user_id);

    $response['new_new_followers'] = $followers;

    $response['old_new_followers'] = $old_followers;

    $response = json_encode( $response );

    echo $response;

    update_user_meta( $user_id, '_tb_old_followed_by_count', $followers + 1);

    die();
}
add_action('wp_ajax_tb_update_old_followers' , 'tb_update_old_followers');

function tb_get_follower_count( $user_id = null ) {

if ( empty( $user_id ) ) {
    $user_id = get_current_user_id();
}

$followed_count = get_user_meta( $user_id, '_tb_followed_by_count', true );

$count = 0;

if ( $followed_count ) {
    $count = $followed_count;
}

return (int) apply_filters( 'tb_get_follower_count', $count, $user_id );
}


function tb_get_old_follower_count( $user_id = null ) {

if ( empty( $user_id ) ) {
    $user_id = get_current_user_id();
}

$old_followed_count = get_user_meta( $user_id, '_tb_old_followed_by_count', true );

$old_count = 0;

if ( $old_followed_count ) {
    $old_count = $old_followed_count;
}

return (int) apply_filters( 'tb_get_old_follower_count', $old_count, $user_id );
}

任何帮助都将不胜感激

原因很简单。您尚未在tb\u update\u old\u followers()函数中定义用户标识。只要这样做,它就会起作用

function tb_update_old_followers() {
    $user_id=get_current_user_id();

    $followers = tb_get_follower_count($user_id);

    $old_followers = tb_get_old_follower_count($user_id);

    $response['new_new_followers'] = $followers;

    $response['old_new_followers'] = $old_followers;

    $response = json_encode( $response );

    echo $response;

    update_user_meta( $user_id, '_tb_old_followed_by_count', $followers + 1);
    die();
}

好的,请稍等。@Luca,现在编辑好了,如果你想看一下。。。感谢“echo$response;”return是什么?@ElvinHaci它返回
$response['new\u new\u followers']=$followers的计数
$response['old\u new\u followers']=$old\u followers但它不会执行除此之外的任何操作。好的,我已经添加了一个答案