0

I am having trouble selecting signup meta, it always returns empty array.

My code is:

add_action( 'wpmu_activate_user', 'promote_to_admin', 99 );
function promote_to_admin( $user_id, $password, $meta ) {
    global $wpdb;
    $user = get_userdata( $user_id );
    $email = $user->user_email;
    var_dump( $email );
    $signup_meta = $wpdb->get_results( "SELECT meta FROM wp_signups WHERE user_email = $email" );
    if ( $signup_meta['new_role'] == 'administrator' ) {
        $user->set_role( 'administrator' );
    }
}

$signup_meta is returned as an empty array. However, when I check the database, the signup_meta array contains values.

I also tried dumping $user and $email - everything is fine, $user object is populated with data and $email is correct.

Anybody know what can be the issue here and how can I retrieve the signup meta?

Scenario is when a new user is added by Admin from Dashboard. My goal is to parse the role that is assigned when new user is created by admin.

It's a bug fixing attempt, since the plugin that I am using to manage custom and default roles (WPFront User Role Editor), overrides the Administrator role assigned to a new user from the Dashboard. So, when a new user is activated I want to check if he was initially assigned Administrator role and update the default role assigned by the plugin.

2

Alright, finally I sorted it out - and it was a query error (embarrassing it is).

Correct code:

add_action( 'wpmu_activate_user', 'promote_to_admin', 99 );
function promote_to_admin( $user_id, $password, $meta ) {
    global $wpdb;
    $user = get_userdata( $user_id );
    $email = $user->user_email;
    // You need to prepare query first to avoid SQL errors
    $query = $wpdb->prepare( "SELECT meta from `wp_signups` where `user_email` = %s", $email );
    // By default, Object(stdClass) will be returned; 
    // tell WordPress to convert it to multidimensional array
    $raw_meta = $wpdb->get_results( $query, ARRAY_A );
    // After conversion, array will be represented as serialized string - unserialize it
    $signup_meta = unserialize( $raw_meta[0]['meta'] );
    if ( array_key_exists( 'new_role', $signup_meta ) ) {
        $user->set_role( $signup_meta['new_role'] );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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