Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wp-user-manager/85c249f5e03d68ae2f4ca651b311f412 to your computer and use it in GitHub Desktop.
Save wp-user-manager/85c249f5e03d68ae2f4ca651b311f412 to your computer and use it in GitHub Desktop.
WP User Manager - Redirect different users to specific pages after login
<?php
add_filter( 'wpum_redirect_after_login', 'my_wpum_redirect_after_login', 10, 2 );
function my_wpum_redirect_after_login( $redirect, $user ) {
if ( $user->ID === 1 ) {
$redirect = home_url( '/some-url/' );
} elseif ( $user->ID === 2 ) {
$redirect = home_url( '/another-url/' );
}
return $redirect;
}
@wp-user-manager
Copy link
Author

wp-user-manager commented May 11, 2020

If you want to define the URL (or part of the URL) per user in the wp-admin, use the Custom Fields addon to create a text field with the permission set to hidden, so only admins can edit the field for users in the backend.

Use the 'Unique meta key' of the field in the code below, with the prefix 'wpum_' eg, wpum_field_8

<?php

add_filter( 'wpum_redirect_after_login', 'my_wpum_redirect_after_login', 10, 2 );
function my_wpum_redirect_after_login( $redirect, $user ) {
	$url = carbon_get_user_meta( $user->ID, 'wpum_field_8' );
        if ( ! $url ) {
            return $redirect;
        }

        // If whole of the URL then return $url
	return home_url( $url );
}

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