Last active
August 25, 2021 16:00
-
-
Save MikeNGarrett/9533303 to your computer and use it in GitHub Desktop.
Redirect WordPress Multisite User Upon Activation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To redirect to the current site instead of the main site, change wp_redirect( network_site_url() ); to wp_redirect( site_url() );