从类中调用wp_Generate_Password()

时间:2014-12-31 作者:LPH

我有一个添加wp用户的功能。有一个$password = wp_generate_password(); 在里面。这是可行的。

现在我正在创建一个类XenWord_Add_WP_User 基于功能。事实上,我复制了该函数以成为一种方法public function xenword_add_wp_user()

然而,这是失败的。错误状态为调用未定义的函数wp\\u generate\\u password()。如何在类中调用wp\\u generate\\u password()?

这是这门课的一部分。

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();
更新:澄清。这就是有效的功能。这是一个函数,我正试图将其移动到一个类中。

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

1 个回复
SO网友:cybmeta

首先,我要说的是,如果在类外使用函数,那么您就错了。这是不正确的,您可以使用它进行验证(它是作为函数编写和执行的类的确切方法:

//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();
经过一点调查,我注意到wp_generate_password() 之前不可用wp 操作,因此您必须将函数挂钩到wp, init wp_loaded 或任何其他适合您需要的动作钩。上述代码不起作用,但例如,此代码起作用:

//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.
}
对于课堂,你可以这样做:

//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.
    }
}

结束

相关推荐