I want to login user automatically when user is activated in multisite.
but when I am trying to use wp_signon()
it response with the error
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\krosswall\wp-includes\class.wp-styles.php:127) in C:\xampp\htdocs\krosswall\wp-includes\pluggable.php on line 955
I am trying to do this
function kw_activate_blogs( $user_id, $password, $meta ) {
add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
$user = new WP_User( (int) $user_id );
$creds = array();
$creds['user_login'] = $user->user_login;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
wp_set_current_user($user->ID);
if ( is_wp_error($user) ) {
echo $user->get_error_message();
} else {
// safe redirect to actually login the user - otherwise they would need to manually refresh the page
// PLUS: this clears the activation confirmation page with the plain text password printed on screen
//wp_safe_redirect( get_home_url() );
exit;
}
}
add_action( 'wpmu_activate_user', 'kw_activate_blogs', 10, 3 );
I added this code in functions.php
file. Please help me to solve it.