代码必须创建一个随机的数字和字母字符串,并将其添加为 wordpress 用户的元字段。此代码导致 User Add 部分输出如下错误:
Fatal error: Uncaught `ArgumentCountError`: Too few arguments to function `myplugin_registration_save()`,
1 passed in <path>/class-wp-hook.php on line 286 and exactly 2 expected in <path>/functions.php:347
Stack trace:
#0 <path>/wp-includes/class-wp-hook.php(286): myplugin_registration_save(51)
#1 <path>/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 <path>/plugin.php(465): WP_Hook->do_action(Array)
#3 <path>/user.php(1857): do_action('user_register', 51)
#4 <path>/user.php(226): wp_insert_user(Array)
#5 <path>/user-new.php(148): edit_user()
#6 {main} thrown in <path>/functions.php on line 347
编码:
function generateRandomString($length = 30) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
add_action('user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save($user_id , $length) {
$meta_key = "pkg_autologin_code";
$timestampz=time();
$tokenparta = generateRandomString();
$key = $timestampz*3 . $tokenparta;
update_user_meta($user_id, $meta_key , $key);
}