主题我的登录验证个人资料页面?

Theme my login validate profile page?

我正在使用主题我的登录 WordPress 插件,使用自定义页面我添加了我自己的注册字段并验证它们没有问题。

但是我担心地发现,一旦用户注册,就可以添加损坏的代码并更新到个人资料页面,我想知道个人资料页面在验证方面是否提供与注册?

最好的方法是使用"theme my login" 模板页面,它已经有注册和个人资料部分。您可以根据自己的喜好添加和删除字段并设置样式,这是一个教程 http://www.jfarthing.com/development/theme-my-login/adding-extra-registration-fields/

如果您有自定义的个人资料页面,那么我建议您使用 wpnonce 检查 POST 请求的有效性。其次使用wordpress自带的函数来获取和更新数据。 get_user_meta 和 update_user_meta 等函数内置了所有验证功能,您不必担心。

编辑:我写了这个代码来演示如何使用随机数字段,然后如何检查随机数字段的有效性(默认情况下,随机数在 24 小时内有效).下面的代码添加了一个表单并询问用户身高。在提出 post 请求之前,第一个 php 部分不会 运行。一旦发出请求,它就会检查请求的完整性。如果满足所有条件,那么它会在数据库中添加一个名为 'user_height' 的新元字段,并且会在用户每次更改时更新。一旦设置了高度,它也会自动填充到输入框中,这样他们就可以看到他们当前的高度是多少。我希望这段代码能解决您对显示用户元数据、adding/updating 用户元数据以及验证随机数的所有疑虑。

<?php
// Checking if the post request has been submitted and then verifing nonce
if (!isset( $_POST['get_user_height'] ) || !wp_verify_nonce( $_POST['get_user_height'], 'user_body_built' ) 
) {
   print 'Sorry, the request cannot be verified.';
   exit;
} else {
   if(isset($_POST['user_height']) && !empty($_POST['user_height'])){
      update_user_meta( $user_id, 'user_height', $_POST['user_height']);
   }
}
<form method="post">
   // Fetching previous height of user
   <?php $user_height = get_user_meta($user_id, 'user_height', TRUE);?>
   // Getting user's height and then saving it to users meta, if height was already set it will also show the current height.
   <input type="text" name="user_height" <?php if($user_height){echo 'value="'.$user_height.'"';} ?> placeholder="enter your height">
  // Generating a nonce field which will be checked on post request
   <?php wp_nonce_field( 'user_body_built', 'get_user_height' ); ?>
</form>

第二次编辑(展示如何使用个人资料页面上的现有注册字段,将输入名称替换为注册页面上的名称):只需将此代码添加到您的个人资料页面或functions.php 它将自动在配置文件页面中显示这些字段。

function tml_edit_user_profile( $profileuser ) {?>
    <p>
        <label for="phone_number">Phone Number</label>
        <!-- replace name attribute with the ones used on registration page -->
        <input id="phone_number" type="text" name="phone_number" value="<?php echo $profileuser->phone_number; ?>" />
    </p>
    <?php
}
add_action( 'edit_user_profile', 'tml_edit_user_profile' );