Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.