这是我第一次接触WP Development,我正在尝试在注册后不显示WP管理栏。因为它是名为“show\\u admin\\u bar-front”的元数据,所以我会在创建用户后立即更新它。
if(count($error) == 0) {
wp_create_user( $username, $password, $email);
$user_id = get_current_user_id();
update_user_meta($user_id, \'show_admin_bar_front\', \'false\');
echo "User Created Successfully!";
exit();
}
如您所见,在上面的区块中,我尝试在创建用户并更新指向的元数据后立即获取用户ID(默认为“true”)。也许我在做一些很愚蠢的事。有人能启发我吗?Bellow,完整代码。
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
if($_POST) {
$username = $wpdb->escape($_POST[\'txtUsername\']);
$email = $wpdb->escape($_POST[\'txtEmail\']);
$password = $wpdb->escape($_POST[\'txtPassword\']);
$ConfPassword = $wpdb->escape($_POST[\'txtConfirmPassword\']);
$error = array();
if(strpos($username, \'\') !==FALSE) {
$error[\'username_space\'] = "Username has space!";
}
if(empty($username)) {
$error[\'username_empty\'] = "Username is empty!";
}
if(username_exists($username)) {
$error[\'username_exists\'] = "Username already exists!";
}
if(!is_email($email)) {
$error[\'email_valid\'] = "Please, add a valid e-mail.";
}
if(email_exists($email)) {
$error[\'email_existence\'] = "This e-mail is already registered.";
}
if(strcmp($password, $ConfPassword) !==0) {
$error[\'password\'] = "Password don\'t match!";
}
if(count($error) == 0) {
wp_create_user( $username, $password, $email);
$user_id = get_current_user_id();
update_user_meta($user_id, \'show_admin_bar_front\', \'false\');
echo "User Created Successfully!";
exit();
} else {
print_r($error);
}
}
?>
<form method="post">
<p>
<label>Username</label>
<div>
<input type="text" id="txtUsername" name="txtUsername" placeholder="Username"/>
</div>
</p>
<p>
<label>Email</label>
<div>
<input type="email" id="txtEmail" name="txtEmail" placeholder="Email"/>
</div>
</p>
<p>
<label>Password</label>
<div>
<input type="password" id="txtPassword" name="txtPassword" placeholder="Password"/>
</div>
</p>
<p>
<label>Confirm Password</label>
<div>
<input type="password" id="txtConfirmPassword" name="txtConfirmPassword" placeholder="Confirm Password"/>
</div>
</p>
<input type="submit" name="btnSubmit"/>
</form>
最合适的回答,由SO网友:Sephsekla 整理而成
如果你想像这样在注册时更新meta,我认为更好的选择是使用register_new_user 钩住add_action. https://developer.wordpress.org/reference/hooks/register_new_user/
比如:
add_action(\'register_new_user\',\'my_update_meta\',10,1);
function my_update_meta($user_id){
update_user_meta($user_id, \'show_admin_bar_front\', false);
}
如果有疑问,挂钩/过滤器/操作往往是WordPress工作的最佳选择。