未定义变量:get_user_meta中的user_id

时间:2021-02-23 作者:Rusydi

使用get\\u user\\u meta时,我得到了未定义的变量:user\\u id

$phone = get_user_meta( $user_id, \'phone_no\', true );
$icno = get_user_meta( $user_id, \'ic_no\', true );
当我试着把上面的代码放在这行下面

$user_id = wp_insert_user( $user_data );
没有发生错误,但我的值$phone和$icno变为空

这是我的完整代码

<?php
/*
Template Name: Register
*/
get_header(); 
// Exit if accessed directly
if ( !defined(\'ABSPATH\')) exit;
?>
<html>
    

<body id="login-page" <?php body_class(); ?>>
<div class="container">
<div class="row register-page-container p-3 p-lg-5 mt-5 d-flex justify-content-center w-75 mx-auto">
<?php
global $wpdb, $user_ID;
    
//Check whether the user is already logged in 
if (!$user_ID) {
// Default page shows register form. 
// To show Login form set query variable action=login
$action = (isset($_GET[\'action\']) ) ? $_GET[\'action\'] : 0;
// Login Page
if ($action === "login") { ?>
<?php 
$login = (isset($_GET[\'login\']) ) ? $_GET[\'login\'] : 0;
if ( $login === "failed" ) {
echo \'<div class="col-12 register-error"><strong>ERROR:</strong> Invalid username and/or password.</div>\';
} elseif ( $login === "empty" ) {
echo \'<div class="col-12 register-error"><strong>ERROR:</strong> Username and/or Password is empty.</div>\';
} elseif ( $login === "false" ) {
echo \'<div class="col-12 register-error"><strong>ERROR:</strong> You are logged out.</div>\';
}
?>
<div class="col-md-5">
<?php 
$args = array(
\'redirect\' => home_url().\'/login/\', 
);
wp_login_form($args); ?>
<p class="text-center"><a class="mr-2" href="<?php echo wp_registration_url(); ?>">Register Now</a>
<span clas="mx-2">·</span><a class="ml-2" href="<?php echo wp_lostpassword_url( ); ?>" title="Lost Password">Lost Password?</a></p>
</div>
<?php
} else { // Register Page ?>

<?php if(get_option(\'users_can_register\')) { ?>
<div class="col-md-5 manual-register-form">
<form action="" method="post"> 
<p> 
<label for="user_login">Username</label>
<input type="text" name="username" placeholder="Type your Username Here" class="register-input mb-4" value="<?php if( ! empty($username) ) echo $username; ?>" /><br />
</p>
<p> 
<label for="user_password">Password</label>
<input type="password" name="password" placeholder="Type your Password Here" class="register-input mb-4" value="<?php if( ! empty($password) ) echo $password; ?>" /><br />
</p>
<p> 
<label for="user_email">Email</label>
<input type="text" name="email" placeholder="Type your Email Here" class="register-input mb-4" value="<?php if( ! empty($email) ) echo $email; ?>" /> <br /> 
</p>
<p> 
<label for="phone_no">Phone</label>
<input type="text" name="phone_no" placeholder="Type your Phone Here" class="register-input mb-4" value="<?php if( ! empty($phone) ) echo $phone; ?>" /> <br /> 
</p>
<p> 
<label for="ic_no">IC No</label>
<input type="text" name="ic_no" placeholder="Type your IC No Here" class="register-input mb-4" value="<?php if( ! empty($icno) ) echo $icno; ?>" /> <br /> 
</p>
<input type="submit" id="register-submit-btn" class="mb-4" name="submit" value="SignUp" /> 
</form>
<p>Already have an account? <a href="/login">Login Here</a></p>
</div>
<?php } else {
echo "Registration is currently disabled. Please try again later."; 
    
}
} ?>
<?php
if ( $_POST ) {
$error = 0;
    
$username = esc_sql($_REQUEST[\'username\']); 
if ( empty($username) ) {
echo \'<div class="col-12 register-error">User name should not be empty.</div>\'; 
$error = 1;
}

$email = esc_sql($_REQUEST[\'email\']);
if ( !preg_match("/^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$/", $email) ) { 
echo \'<div class="col-12 register-error">Please enter a valid email.</div>\';
$error = 1;
}
    
$phone = esc_sql($_REQUEST[\'phone_no\']);
if ( empty($phone) ) { 
echo \'<div class="col-12 register-error">Please enter a valid phone.</div>\';
$error = 1;
}
    
$error = 0; 
$icno = esc_sql($_REQUEST[\'ic_no\']);
if ( empty($icno) ) { 
echo \'<div class="col-12 register-error">Please enter a valid IC No.</div>\';
$error = 1;
}   
    
if ( $error == 0 ) {
$password = esc_sql($_REQUEST[\'password\']); 
if ( empty($password) ) {
echo \'<div class="col-12 register-error">Password should not be empty.</div>\'; 
$error = 1;
}
$phone = get_user_meta( $user_id, \'phone_no\', true );
$icno = get_user_meta( $user_id, \'ic_no\', true );

$username  = $_POST[\'username\'];
$password  = $_POST[\'password\'];
$email   = $_POST[\'email\'];
$icno   = $_POST[\'ic_no\'];
$phone   = $_POST[\'phone_no\'];

    $user_data = [
    \'user_login\' => $username,
    \'user_pass\'  => $password,
    \'user_email\'   => $email,

];
 
$user_id = wp_insert_user( $user_data );

update_user_meta( $user_id, \'phone_no\', $phone );
update_user_meta( $user_id, \'ic_no\', $icno ); 

// // success
if ( ! is_wp_error( $user_id ) ) {
    echo \'User created: \'. $user_id;
}   
 
 else {
 echo \'Registration error\';
}
}
}
if ( $error != 2 ) { ?> 
<?php  }
} else { ?>
<p>You are logged in. Click <a href="<?php bloginfo(\'wpurl\'); ?>">here to go home</a></p>
<?php } ?>
</div>
</div>
<?php get_footer(); 
?>
</body>
</html>

1 个回复
SO网友:Tom J Nowell

使用get\\u user\\u meta时,我得到了未定义的变量:user\\u id

这是因为$user_id 为空且未定义,则需要为其指定一个值,该值需要是用户的ID。

当我试着把上面的代码放在这行下面

没有发生错误,但我的值$phone和$icno变为空

是的,你给了$user_id 一个有效的价值。phone和icno为空的原因是phone和icno没有用户元。The order you do things in matters. 代码从文件的顶部到底部执行。

不能在添加值之前检索值,也不能在设置变量之前使用变量。

2最终注释:

缩进代码很重要,尤其是当你与其他人共享代码时,它可以消除一整类错误global $wpdb, $user_ID; 应移除。代码从不使用$wpdb 而且没有$user_ID 全局变量,除非setup_userdata 被调用

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register