• Resolved melanie bund

    (@melanie-bund)


    Hi am trying to redirect specific users to a custom page, have tried to add different code snippets to my functions page, some dont work others send all users there – below is i presume the basic code which does not seem to work though it should?
    ′// Redirect editor
    function wp_redirect_users_by_role() {

    $current_user = wp_get_current_user();
    $role_name = $current_user->roles[0];

    if ( ‘editor’ === $role_name ) {
    wp_redirect( ‘https://photohunters.org/add-star’ );
    if ( ‘administrator’ === $role_name ) {
    wp_redirect( ‘https://photohunters.org/wp-admin’ );
    } // if

    } // wp_redirect_users_by_role
    add_action( ‘admin_init’, ‘wp_redirect_users_by_role’ ); ′

    thanks Melanie

Viewing 8 replies - 1 through 8 (of 8 total)
  • bdbrown

    (@bdbrown)

    Hi Melanie. Not sure if this is it but it appears that you’re missing a closing bracket } at the end of your first “if” statement.

    Thread Starter melanie bund

    (@melanie-bund)

    thank you! bdbrown
    Melanie ??

    Thread Starter melanie bund

    (@melanie-bund)

    still not working – both redirected to homepage

    // Redirect editor
    function wp_redirect_users_by_role() {
    
    $current_user = wp_get_current_user();
    $role_name = $current_user->roles[0];
    
    if ( 'editor' === $role_name ) {
    wp_redirect( 'https://photohunters.org/add-star' );
    } // if
    if ( 'administrator' === $role_name ) {
    wp_redirect( 'https://photohunters.org/wp-admin' );
    }
    
    } // wp_redirect_users_by_role
    add_action( 'admin_init', 'wp_redirect_users_by_role' );

    Thread Starter melanie bund

    (@melanie-bund)

    Hi Am trying various scripts but end up on home page
    here is latest

    // redirect editor
     function editor_redirect( $redirect, $user ){
    	$role = $user->roles[0];
    	$addstar = get_permalink( get_page_id( '/add-star/' ) );
    	if( $role == 'editor' )
    	$redirect = $addstar;}

    still no joy
    does anyone have a ny ideas?

    Moderator bcworkz

    (@bcworkz)

    Your next to last snippet works in part. The redirects all need to be permalinks to WP content. Redirects to actual PHP pages or folders will not work. If need be, a custom page template could serve as a shell for a particular backend page, just so you have a permalink to redirect to.

    The thing is though, once the redirects work correctly, the user is then locked into that page for any backend request. That may be fine for low capability users, but editors and administrators would not be happy with such an arrangement.

    Thread Starter melanie bund

    (@melanie-bund)

    Hi bcworkz, that is my point, the “editor” is only able to go to this page and has to log out once his job is finished, only he/she can enter this page as if someone without “editor” role enters they are sent away. the permalink is correct the page is called “add-star”, but it doesnt work, they get directed to the homepage???
    thanks
    Melanie

    Moderator bcworkz

    (@bcworkz)

    If redirects to a permalink are not working for you, I have to think there is some plugin or theme conflict at work in your installation, because they do work with mine, which is nearly “out of the box” condition. If you were to reduce your installation to such a state (deactivate all plugins and switch to a twenty something theme), you should find at least the editor redirect will work.

    You can then restore your theme and plugins, one by one, checking the redirect each time. Once it fails again, the last added component is the trouble maker. You could then perhaps take up the issue with the component’s author.

    Thread Starter melanie bund

    (@melanie-bund)

    thank you bcworkz, will have a go, Melanie

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘redirect user by role’ is closed to new replies.