gpt4 book ai didi

php - 如何检查metakey是否存在wordpress

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:24 25 4
gpt4 key购买 nike

基本上我想检查 wordpress 的元数据中是否存在一行。如果该用户的元键表不存在,它应该创建一个。我现在正在使用的这段代码可以正常工作,但似乎有问题。我最近注意到一些用户现在有两到五个名为 credits 的 meta_key,其值为 0。有没有更好的方法来检查用户是否已经存在 meta_key?

<?php  
global $current_user;
global $wpdb;



$useridvip=$current_user->ID;
$credits=$current_user->credits;

if (empty($credits)) {

$wpdb->insert( "wp_usermeta", array( 'umeta_id' => null, 'user_id' => $useridvip, 'meta_key' => credits, 'meta_value' => 0 ) );
$credits="0";
}

else { echo "all good"; }

最佳答案

尚未对此进行测试,但我要做的第一件事就是确保将其包装在 is_user_logged_in() 中

if(is_user_logged_in()) {

//then put your check/update code here

}

在那里面,你可以尝试...

global $current_user;

$useridvip=$current_user->ID;
$credits = get_user_meta($useridvip, 'credits', true);

if($credits == '') {

add_user_meta( $useridvip, 'credits', 0, true );

}

一般来说,您应该尽可能依赖 get_user_meta 和 add_user_meta。

关于php - 如何检查metakey是否存在wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23681799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com