Call wp_generate_password() from within a Class

Question

I have a function to add wp users. There is a $password = wp_generate_password(); in it. This works.

Now I’m creating a class XenWord_Add_WP_User based on the function. In fact, I copied the function to become a method public function xenword_add_wp_user()

However, this is failing. The error states call to undefined function wp_generate_password(). How can I call wp_generate_password() within a class?

Here is a portion of the class.

Class XenWord_Add_WP_User {

public function xenword_add_wp_user() {
    /**
     * @since 1.0.6
     * Insert the current user into the wp_users and wp_usermeta table
     *
     * @since 1.0.7
     * Moved this code to a separate function.
     *
     * TODO: This code needs to be cleaned up
     */

    global $wpdb;

    $xenword_options = get_option( 'xenword_options' );

    $visitor = XenForo_Visitor::getInstance();

    $user_ID = $visitor->get( 'user_id' );

    /* Added 2.0.1.02 */
    $wp_default_role = $xenword_options['wp_default_role'];

    $user_check = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users WHERE ID = '$user_ID' " );
    $user_id  = $visitor->get( 'user_id' );
    $password = wp_generate_password();
    $nickname = $visitor->get( 'username' );
    $email    = $visitor->get( 'email' );
    $current_registration = date('Y/m/d H:i:s', $visitor->get('register_date'));
// removed the rest of the class in this post since the failure is a few lines up.
    }
}
$wp_add_user = new XenWord_Add_WP_User();

UPDATE: To clarify. This is the function which works. It is a function that I am trying to move into a class.

function xenword_add_wp_user() {
/**
 * @since 1.0.6
 * Insert the current user into the wp_users and wp_usermeta table
 *
 * @since 1.0.7
 * Moved this code to a separate function.
 *
 * TODO: This code needs to be cleaned up
 */

global $wpdb;

$xenword_options = get_option( 'xenword_options' );

$visitor = XenForo_Visitor::getInstance();

$user_ID = $visitor->get( 'user_id' );

/* Added 2.0.1.02 */
$wp_default_role = $xenword_options['wp_default_role'];

$user_check
                      = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users WHERE ID = '$user_ID' " );
$user_id              = $visitor->get( 'user_id' );
$password             = wp_generate_password();
$nickname             = $visitor->get( 'username' );
$email                = $visitor->get( 'email' );
$current_registration = date( 'Y/m/d H:i:s',
    $visitor->get( 'register_date' ) );

if ( null == username_exists( $email ) ) {

    $new_userdata = array(
        'ID'              => $user_id,
        'xf_user_id'      => $user_id,
        'user_login'      => $nickname,
        'user_pass'       => $password,
        'user_nicename'   => $nickname,
        'user_email'      => $email,
        'user_registered' => $current_registration,
        'display_name'    => $nickname
    );

    /*
     * Added 2.0.1.02
     * Added default role chosen in XenWord panel
     */
    $role_userdata = array(
        'ID'              => $user_id,
        'xf_user_id'      => $user_id,
        'role'            => $wp_default_role,
        'user_login'      => $nickname,
        'user_pass'       => $password,
        'user_nicename'   => $nickname,
        'user_email'      => $email,
        'user_registered' => $current_registration,
        'display_name'    => $nickname
    );

    if ( $user_check == 1 ) {

        /**
         * wp_update_user resets cookies leading to a nonce error in the WP admin panel.
         */
        // wp_update_user( $new_userdata );

    } else {

        /* Insert userdata into prefix_users table */
        $table = $wpdb->prefix . 'users';

        $wpdb->insert( $table, $new_userdata );

        // $wpdb->show_errors();

        // $wpdb->last_query;

        // exit( var_dump( $wpdb->last_query ) );

        /* Insert into usermeta table */
        wp_insert_user( $role_userdata );

        if ( $xenword_options['use_wordpress_toolbar'] == true ) {
            update_user_meta( $user_id, 'show_admin_bar_front',
                'true' );
        } else {
            update_user_meta( $user_id, 'show_admin_bar_front',
                'false' );
        }
     }
   }
}

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/call-wp_generate_password-from-within-a-class/">

Call wp_generate_password() from within a Class</a>
0
LPH 5 months 0 Answers 37 views 0

Leave an answer