1

我正在尝试更改wp_set_password可插入功能并向其添加自定义操作:

function wp_set_password( $password, $user_id ) {
    // Keep original WP code
    global $wpdb;

    $hash = wp_hash_password( $password );
    $wpdb->update(
        $wpdb->users,
        array(
            'user_pass'           => $hash,
            'user_activation_key' => '',
        ),
        array( 'ID' => $user_id )
    );

    wp_cache_delete( $user_id, 'users' );

    // and now add your own
    $custom_hash = password_hash( $password, PASSWORD_DEFAULT );
    update_user_meta($user_id, 'user_pass2', $custom_hash);
}

我将此代码放在我的自定义插件中,但它不会运行我在其中编写的自定义操作。我不确定问题是什么。

也许我把它放在错误的位置,或者我应该在某个地方调用它?

如何wp_set_password()使用 WooCommerce 连接 WordPress 功能?


编辑

这段代码根本不会触发,我尝试在 users 表中输入相同的密码,但它不关心我的代码并执行默认操作。


编辑 2

我注释了插件中的代码并更改了文件夹pluggable.php中的主文件wp-includes,并添加了这两行。

$custom_hash = $password;
update_user_meta($user_id, 'user_pass2', $custom_hash);

但它仍然不起作用。


编辑 3

我什至从 中删除了整个功能pluggable.php,它仍然有效!我为新用户创建了用户名和密码。

应该是 WooCommerce 注册。我使用 WooCommerce 登录系统。


编辑 4

我使用了 WordPress 注册系统/wp-login.php,这段代码终于可以工作了。

现在我想知道 WooCommerce 在哪里以及如何实现这样的目标,wp_usermeta用自定义的东西更新表格。

4

1 回答 1

1

您应该始终避免覆盖任何核心文件,因为当 WordPress 更新时您会丢失您的更改,并且您可能会在相关的合理过程中遇到大麻烦。

您可以尝试使用WC_Customer()Class 可用的 setter 方法,例如:

// Get an instance of the WC_Customer Object from the user ID
$customer = new WC_Customer( $user_id );

// Set password
$customer->set_password( $password );

// Set other metadata
$customer->set_first_name( $first_name );
$customer->set_last_name( $last_name );

// Save to database (and sync cached data)
$customer->save();

您可以使用 Class 中的 Woocommerce相关挂钩WC_Customer_Data_Store


添加- 使用 Woocommerce 创建客户:

1)您可以使用wc_create_new_customer()返回用户ID的函数。

2) 或者您可以使用 Object 的空实例,在WC_Customer其上使用任何可用的 setter 方法(它会在最后返回用户 ID)

// Get an empty instance of the WC_Customer Object
$customer = new WC_Customer();

// Set username and display name
$customer->set_username( $user_name );
$customer->set_display_name( string $display_name )

// Set user email
$customer->set_email( $user_email );
$customer->set_display_name( $display_name );

// Set First name and last name
$customer->set_first_name( $first_name );
$customer->set_billing_first_name( $first_name );
$customer->set_last_name( $last_name );
$customer->set_billing_last_name( $last_name );

// Set password
$customer->set_password( $password );

// Set other metadata
$customer->set_billing_first_name( $first_name );
$customer->set_billing_last_name( $last_name );

// Save to database - returns the User ID (or a WP Error) 
$user_id = $customer->save();
于 2019-03-31T23:23:53.273 回答