Create a function in WP for when it is redirected

0

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

$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 {

}
    
asked by anonymous 11.04.2017 / 23:20

1 answer

1

There is a filter (read about filters here) in WordPress called login_redirect . In a very brief way, filters allow you to perform certain actions on data "events" 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 ordinary users to the main 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 warns that the $user variable may not be available at runtime, and suggests using global $user . So by making a simplified use case, you can 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 wp_login_form() method, you can use the redirect parameter that it contains.

There is still the wp_redirect() method that does what it says. Just be aware to use exit() at the end.

    
12.04.2017 / 13:59