mercredi 19 juin 2019

Problem with generating random string and save into user meta field in wordpress

I've this code that doesn't want work. The code must create a random string of numbers and letters and add this as meta field of wordpress user. This code cause to User Add section errors like:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function myplugin_registration_save(), 1 passed in */class-wp-hook.php on line 286 and exactly 2 expected in /functions.php:347 Stack trace: #0 /wp-includes/class-wp-hook.php(286): myplugin_registration_save(51) #1 /wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /plugin.php(465): WP_Hook->do_action(Array) #3 /user.php(1857): do_action('user_register', 51) #4 /user.php(226): wp_insert_user(Array) #5 */user-new.php(148): edit_user() #6 {main} thrown in ***/functions.php on line 347

(***/ hide the path)

the code:

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);
    }

Thankyou




Aucun commentaire:

Enregistrer un commentaire