Create a role in WP for when to log in to be redirected

Asked

Viewed 137 times

0

I need to create a function in Wordpress so that when the user logs in automatically is redirected to another link, here is my code as is:

$user_id = get_current_user_id();
if ($user_id == 2) { //aqui adicionamos o ID do usuário cadastrado

    function remove_menus(){

        remove_menu_page( 'index.php' ); 
        remove_menu_page( 'about.php' );
        remove_menu_page( 'edit.php' ); 
        remove_menu_page( 'upload.php' ); 
        remove_menu_page( 'edit.php?post_type=page' );
        remove_menu_page( 'edit-comments.php' ); 
        remove_menu_page( 'themes.php' ); 
        remove_menu_page( 'users.php' ); 
        remove_menu_page( 'tools.php' ); 
        remove_menu_page( 'options-general.php' ); 
        remove_menu_page( 'admin.php?page=revslider' ); 
        remove_menu_page( 'admin.php?page=wpcf7' ); 
        remove_menu_page( 'admin.php?page=loco' );  
    }

//aqui é onde tento criar a função para fazer esse redirecionamento mais não funcionou
    function realocar (){
        if (isset($_POST[get_current_user_id == 2])){
            $url = 'aqui vem o link pra redirecionar';
            header("Location: $url");
        }

    }

    add_action( 'admin_menu', 'remove_menus', 'realocar' );


} else {

}
  • Avoid touching the wordpress code directly. If you do not know what you are doing ends up breaking the script or loses everything in an update or worse. Try using a plugin, like this one: https://br.wordpress.org/plugins/redirect-after-login/ But if you have to tweak it, then you have to add a filter in wp-login.php

  • @Renefreak I am creating a plugin to do this, otherwise my work may get lost in any WP update that comes up.

1 answer

1


There is a filter (read about filters here) in Wordpress called login_redirect. In a very short way, filters allow you to perform certain actions on "events" data that occur in the WP stream. The very description of this particular filter is

The login_redirect filter is used to change the Location redirected to after logging in. This could be the Location set by the "redirect_to" Parameter sent to the login page.

That is, you can use it to redirect the user after login. The example that comes from the documentation serves to direct the admins to the Dashboard, and common users for the home page. See:

function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

The documentation still alerts to the fact that the variable $user may not be available at the time of execution, and suggests using global $user. So by making a simplified use case, you might have something like this

function meu_redirect() {

    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //usuário existe
        return "http://meusite.com";
    }
}

add_filter( 'login_redirect', 'meu_redirect', 10, 3 );

BONUS:

If you are using the method wp_login_form(), you can use the parameter redirect that it contains.

There is still the method wp_redirect(), that does well what it says. Just stay tuned to use the exit() in the end.

  • thank you very much, well explained, take me a doubt the 3 o 10 is the user ID?

  • add_filter has four parameters, and the last two refer to the execution priority and the number of arguments respectively

  • Got it, now if I just wanted the ID 2 user to be redirected as this script would look: Function meu_redirect() { global $user; if ( isset( $user->roles ) && is_array( $user->roles ) { ///user exists Return "http://meusite.com"; } } add_filter( 'login_redirect', 'meu_redirect', 10, 3 );

  • 1

    if(get_current_user_id() == "2") { ... }

  • Although its logic is correct Caio, and I understood perfectly, but in practice only worked even the very example of wordpress that redirects the user to the main page of the site, I could not make the redirection to an external page, is that I’m doing something wrong?

  • @Wpfan may be some problem with the filter parameters, but I’m not sure. That 3 at the end says the method meu_redirect() should receive up to 3 different parameters. Do some tests to see what happens

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.