Rufen Sie wp_generate_password () innerhalb einer class auf

Ich habe eine function, um wp Benutzer hinzuzufügen. Es gibt ein $password = wp_generate_password(); drin. Das funktioniert.

Jetzt erstelle ich eine class XenWord_Add_WP_User basierend auf der function. In der Tat habe ich die function kopiert, um eine Methode public function xenword_add_wp_user()

Dies ist jedoch fehlgeschlagen. Die Fehlerzustände rufen die undefinierte function wp_generate_password () auf. Wie kann ich wp_generate_password () innerhalb einer class aufrufen?

Hier ist ein Teil der 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: Um zu klären. Dies ist die function, die funktioniert. Es ist eine function, die ich versuche, in eine class zu bringen.

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

Solutions Collecting From Web of "Rufen Sie wp_generate_password () innerhalb einer class auf"

Lassen Sie mich zunächst sagen, dass Sie sich irren, wenn Sie sagen, dass die function funktioniert, wenn Sie außerhalb einer class arbeiten. Das ist nicht korrekt und Sie können dies überprüfen (es ist die genaue Methode Ihrer class geschrieben und als function ausgeführt:

 //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(); 

Nach wp_generate_password() Untersuchung habe ich festgestellt, dass wp_generate_password() nicht vor der Aktion wp verfügbar ist. Sie müssen also die function an wp , init wp_loaded oder an einen anderen Aktions-Hook wp_loaded , der Ihren Anforderungen entspricht. Der obige Code funktioniert nicht, aber dieser Code beispielsweise:

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

Und für den Unterricht können Sie so etwas tun:

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