最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Call wp_generate_password() from within a Class

programmeradmin0浏览0评论

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

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 Improve this question edited Jan 3, 2015 at 16:17 LPH asked Dec 31, 2014 at 6:07 LPHLPH 8081 gold badge11 silver badges25 bronze badges 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 Commented 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 Commented 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 Commented 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 Commented 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 Commented Jan 3, 2015 at 16:18
 |  Show 2 more comments

1 Answer 1

Reset to default 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.
    }
}
发布评论

评论列表(0)

  1. 暂无评论