Personal After-Login Page for Each User
Adds an "After login, go to" field to each user's profile so administrators can send specific clients or members to their own page when they log in.
Opens WPIDE on
Code
add_action( 'edit_user_profile', function ( $user ) {
if ( ! current_user_can( 'edit_users' ) ) {
return;
}
wp_nonce_field( 'wpide_user_landing_' . $user->ID, 'wpide_user_landing_nonce' );
echo '<h2>After login</h2><table class="form-table" role="presentation"><tr><th scope="row"><label for="wpide-user-landing">Go to page</label></th><td>';
wp_dropdown_pages( array(
'name' => 'wpide_user_landing',
'id' => 'wpide-user-landing',
'selected' => (int) get_user_meta( $user->ID, 'wpide_user_landing', true ),
'show_option_none' => 'Default destination',
'option_none_value' => 0,
'post_status' => array( 'publish', 'private' ),
) );
echo '</td></tr></table>';
} );
add_action( 'edit_user_profile_update', function ( $user_id ) {
if ( ! isset( $_POST['wpide_user_landing_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_user_landing_nonce'] ), 'wpide_user_landing_' . $user_id ) || ! current_user_can( 'edit_users' ) ) {
return;
}
$page = isset( $_POST['wpide_user_landing'] ) ? absint( $_POST['wpide_user_landing'] ) : 0;
if ( $page ) {
update_user_meta( $user_id, 'wpide_user_landing', $page );
} else {
delete_user_meta( $user_id, 'wpide_user_landing' );
}
} );
add_filter( 'login_redirect', function ( $redirect, $requested, $user ) {
if ( ! $user instanceof WP_User ) {
return $redirect;
}
$page = (int) get_user_meta( $user->ID, 'wpide_user_landing', true );
return $page && get_post_status( $page ) ? get_permalink( $page ) : $redirect;
}, 30, 3 );
Notes
Only administrators can set the page. Users without one keep the normal login destination.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.