Wordpress - 在用户注册时将自定义字段添加到数据库

Wordpress - Add custom field to database while user sign up

我在数据库中的 wp_user table 中创建了另一列,我试图以不同的散列方式存储密码以用作另一个应用程序的凭据

password_hash($password, PASSWORD_DEFAULT);

我应该在哪里添加代码以在散列之前使用输入字段数据?

在用户注册之前添加此命令很重要,因为我需要使用用户输入字段中的原始密码

wordpress 有一个名为 user_register 的动作挂钩,它会在用户插入 db 后立即触发。

示例来自 codex

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

和你的功能

function myplugin_registration_save( $user_id ) {

   if ( isset( $_POST['first_name'] ) )
        update_user_meta($user_id, 'first_name', $_POST['first_name']);

}

编辑我

评论后:首先 - 您可以随时使用其他方法散列或更改密码 - 但我不会深入讨论,因此如果您需要更早的操作,您可以使用 register_form to manipulate the form itself , or you can use register_post to manipulate or handle post data from a user registration before the register_errors 过滤器 -根据您的情况,您也可以使用它。

所有这些额外功能都链接在 user_register codex 页面中。

编辑二

评论后:

think I should edit register_form, which file exactly?

从不 从不 编辑核心文件。

这是关于开发人员可以做的最糟糕的做法,原因有很多,我什至无法在这里开始列举。 这正是制作钩子/过滤器的原因。

如果您已经知道,请使用它们,如果您不知道 - 您可以阅读更多内容 here

本例中的挂钩 register_form 正是用于操作表单,您可以在不触及核心文件的情况下更改表单。

编辑三

另请检查函数:wp_hash_password and wp_set_password 允许更改默认哈希算法。

if ( !function_exists('wp_hash_password') ){
    function wp_hash_password($password) {
                //your own hashing algorithm here
            return $password;
    } }

如果这样做,您还需要更改 wp_check_password

if ( !function_exists('wp_check_password') ){
    function wp_check_password($password, $hash, $user_id = '') {
            // do you hash checking
            return apply_filters('check_password', $check, $password, $hash, $user_id);
            }
}