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