Skip to content

Instantly share code, notes, and snippets.

@MikeNGarrett
Last active August 25, 2021 16:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikeNGarrett/9533303 to your computer and use it in GitHub Desktop.
Save MikeNGarrett/9533303 to your computer and use it in GitHub Desktop.
Redirect WordPress Multisite User Upon Activation
<?php
// add do_action('my_do_active_user_message_hook'); where you want the welcome message to display after redirect
add_action( 'activate_header', 'check_activation_key_redirect_to_page' );
/**
* Check the wp-activate key and redirect the user to the apply page
* based on http://www.vanbodevelops.com/tutorials/how-to-skip-the-activation-page-and-send-the-user-straight-to-the-home-page-for-wordpress-multisite
*/
function check_activation_key_redirect_to_page() {
// We check if the key is not empty
if ( ! empty($_GET['key']) || ! empty($_POST['key']) ) {
$key = !empty($_GET['key']) ? $_GET['key'] : $_POST['key'];
// Activates the user and send user/pass in an email
$result = wpmu_activate_signup($key);
if ( ! is_wp_error($result) ) {
extract($result);
$user = get_userdata( (int) $user_id);
// Save the user object to the session
setcookie('my_active_user_variable', json_encode($user));
// Redirect to the network home url
wp_redirect( network_site_url() );
exit;
}
}
}
add_action('my_do_active_user_message_hook', 'check_header_for_active_user');
/**
* Check for the set activated user and echo a message on the home page
*/
function check_header_for_active_user() {
if ( isset($_COOKIE['my_active_user_variable']) && ! empty( $_COOKIE['my_active_user_variable'] ) ) {
$user = json_decode($_COOKIE['my_active_user_variable']);
echo '<div class="app-msg">'. sprintf( __( 'Dear %s. Your account is now active!' ), $user->user_login ).'</div>';
// Unset the session variable since we don't needed anymore
unset($_COOKIE['my_active_user_variable']);
}
}
@govindak
Copy link

how to redirect to subdomain homepage instead of main site ? any idea psl

@reubenbrown13
Copy link

To redirect to the current site instead of the main site, change wp_redirect( network_site_url() ); to wp_redirect( site_url() );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment