1

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' );
        }
     }
   }
}
7
  • Your problem is not using wp_generate_password() inside or outside a class; It seems that you are executing that code outside WordPress, maybe in XenForo?
    – cybmeta
    Dec 31, 2014 at 8:26
  • The code shown is a class inside a WordPress plugin. wp_generate_password() works within a php file simply within a function but will not work once placed inside a class.
    – LPH
    Dec 31, 2014 at 17:11
  • The undefined function error can not be generated because you use the function inside/outside a class but because you execute that PHP script inside/outside WordPress. If WordPress engine is not loaded, WordPress functions are not available. When I say "outside WordPress" I mean that you try to execute a PHP script that depends on WordPress but without loading WordPress first, I doesn't refer the location of the file. I hope you understand.
    – cybmeta
    Jan 1, 2015 at 11:41
  • I'm sorry but you are misunderstanding my question and apparently I'm not clarifying things for you. The file is in a plugin and wp_generate_password works if in a function and not in a class. Once I put the same exact code into a class and make the procedural function into a class method then wp_generate_password fails. Let me try to be clear: xenword_add_wp_user() works if just a function in the file. Now I use the same file (because I'm converting that file to using a class, wrap the xenword_add_wp_user() function around with the words XenWord_Add_WP_User { ... } then I receive the error.
    – LPH
    Jan 2, 2015 at 15:53
  • I have updated the OP to show the working function. The first sentence of the OP is very important. I have a working function that I'm trying to make into a class. The function works alone. If I move it into a class then wp_generate_password fails.
    – LPH
    Jan 3, 2015 at 16:18

1 Answer 1

1

First, let me say that you are wrong saying that the function works if you use outside a class. That is not correct and you can verify using this (it is the exact method of your class written and executed as function:

//Define the function
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.
}
//Exceute the function
xenword_add_wp_user();

After investigate a little, I've noticed that wp_generate_password() is not available before wp action, so you have hook the function to wp, init wp_loaded or any other action hook that fits your needs. The above code doesn't work but, for example, this code does:

//Hook the function to wp_loaded action
add_action('wp_loaded', 'xenword_add_wp_user');
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.
}

And for class you can do something like this:

//Hook the function to wp_loaded action
add_action('wp_loaded', function () {
    $wp_add_user = new XenWord_Add_WP_User;
});
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.
    }
}
4
  • The code I referenced in the OP works on hundreds of installations (not in a class). I'm trying to get things to work in a class. I see my OP does not show the __construct which includes the add action. It's been a long day.
    – LPH
    Jan 3, 2015 at 8:15
  • I'm confused. I don't understand what you mean with "works on hundreds of installations (not in a class)". You posted a question specifically about working in class and you posted a no working code, I tested it and post a working version. Please, edit the question and add any relevant information or it will be impossible to give you a good answer and we will be just wasting the time. Anyway, in your code new XenWord_Add_WP_User is not hooked in any action as I suggested. Try to do it.
    – cybmeta
    Jan 3, 2015 at 9:26
  • I knew you were confused because you are misunderstanding my first sentence in the OP. It clearly states ... "I have a function to add wp users. There is a $password = wp_generate_password(); in it. This works" .... And as I just posted to your code .. .my original code as a procedural function works on hundreds of sites. .. HERE IS THE POINT... Now, I want to move that working function into a class. The class code fails with the statement call to undefined function wp_generate_password().
    – LPH
    Jan 3, 2015 at 16:12
  • My answer still fix your problem. Hook the new XenWord_Add_WP_User in wp or on a later action hook, I don't see the difference after you updated the question.
    – cybmeta
    Jan 3, 2015 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.