Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正确使用更新\用户\元_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Php 正确使用更新\用户\元

Php 正确使用更新\用户\元,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我很难理解如何正确使用update\u user\u meta。现在我有两个函数可以更新用户meta_键。但是,theme\u levels函数每次都会覆盖ajax函数,这可能是因为wp\u head过滤器的缘故。有更好的办法吗 我的代码如下 function theme_levels() { $value = user_total_posts( get_current_user_id() ) if( $value >= '0' && $value &l

我很难理解如何正确使用
update\u user\u meta
。现在我有两个函数可以更新用户meta_键。但是,
theme\u levels
函数每次都会覆盖ajax函数,这可能是因为
wp\u head
过滤器的缘故。有更好的办法吗

我的代码如下

function theme_levels() {

    $value = user_total_posts( get_current_user_id() )

    if( $value >= '0' && $value <= '499' ) {        
        $levels[] = array(
                'level'         => 'level-1',               
        );
        update_user_meta( $user_id, 'author_level', $levels );         
    } else {
        ...
    }
}
add_action( 'wp_head', 'theme_levels' ); 
这就是我所希望的(它也是
$levels
上的
var_dump
):


非常感谢您的帮助。

您正在使用的
$user\u id
没有在任何地方定义

请在开始时在函数
theme\u levels()
ajax\u callback()
中使用以下代码

$current_user = wp_get_current_user();
$user_id=$current_user->ID;

嗨,Rafin,在我的php文件中,
$user\u id
被定义为
get\u current\u user\u id()。谢谢你指出这一点。尽管如此,我的问题仍然存在。如何阻止
theme\u levels()
函数重写AJAX函数?
array(1) { 
    [0]=> array(2) { 
        ["level"]           => string(10) "level-1" // this will be added if user is logged in and reached the level
        ["notification"]    => string(28) "level-1-notification-read" // this should be added after the AJAX call
    } 
}
$current_user = wp_get_current_user();
$user_id=$current_user->ID;