• Resolved kseniyasqo

    (@kseniyasqo)


    Hello,

    I am having troubles 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? Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter kseniyasqo

    (@kseniyasqo)

    Update:
    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, 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.

    Really, it drives me crazy; I can successfully select meta from wp_signups if I run the query directly on the database from phpmyadmin (when user is not yet activated and after he is activated). But programmatically I always get empty array. What the heck?!

    Thread Starter kseniyasqo

    (@kseniyasqo)

    I sorted it out, query error is was:
    Working 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']);
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cannot select signup_meta upon multisite user activation’ is closed to new replies.