Redirect after Login on WordPress

后端 未结 13 3027
我寻月下人不归
我寻月下人不归 2020-12-04 11:26

I\'m creating a customized WordPress theme based on an existing site.

I want to use an alternate dashboard which I have created.

How can I have the user dire

相关标签:
13条回答
  • 2020-12-04 11:49

    This may help. Peter's Login Redirect

    Redirect users to different locations after logging in and logging out.

    Define a set of redirect rules for specific users, users with specific roles, users with specific capabilities, and a blanket rule for all other users. Also, set a redirect URL for post-registration. This is all managed in Settings > Login/logout redirects.

    You can use the syntax [variable]username[/variable] in your URLs so that the system will build a dynamic URL upon each login, replacing that text with the user's username. In addition to username, there is "userslug", "homeurl", "siteurl", "postid-23", "http_referer" and you can also add your own custom URL "variables"...

    0 讨论(0)
  • 2020-12-04 11:49
    add_action('wp_head','redirect_admin');
    function redirect_admin(){
      if(is_admin()){
        wp_redirect(WP_HOME.'/news.php');
        die; // You have to die here
      }
    }
    

    Or if you only want to redirect other users:

    add_action('wp_head','redirect_admin');
    function redirect_admin(){
      if(is_admin()&&!current_user_can('level_10')){
        wp_redirect(WP_HOME.'/news.php');
        die; // You have to die here
      }
    }
    
    0 讨论(0)
  • 2020-12-04 11:52

    The functions.php file doesn't have anything to do with login redirect, what you should be considering it's the wp-login.php file, you can actually change the entire login interface from there, and force users to redirect to your custom pages instead of the /wp-admin/ directory.

    Open the file with Notepad if using Windows or any text editor, Prese Ctrl + F (on window) Find "wp-admin/" and change it to the folder you want it to redirect to after login, still on the same file Press Ctrl + F, find "admin_url" and the change the file name, the default file name there is "profile.php"...after just save and give a try.

    if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) )
            $redirect_to = admin_url('profile.php');
        wp_safe_redirect($redirect_to);
        exit();
    

    Or you can use the "registration-login plugin" http://wordpress.org/extend/plugins/registration-login/, just simple edit the redirect urls and the links to where you want it to redirect after login, and you've got your very own custom profile.

    0 讨论(0)
  • 2020-12-04 11:57

    If you have php 5.3+, you can use an anonymous function like so:

    add_filter( 'login_redirect', function() { return site_url('news'); } );
    
    0 讨论(0)
  • 2020-12-04 11:58
    // Used theme's functions.php  
    add_action('login_form', 'redirect_after_login'); 
    function redirect_after_login() 
    {     
    global $redirect_to; 
    if   (!isset($_GET['redirect_to'])) 
    { 
    $redirect_to =   get_option('sample-page');
    //  sample-page = your page name after site_url
    } }
    
    0 讨论(0)
  • 2020-12-04 12:01

    The accepted answer is very wrong. One should never be modifying the WordPress Core. Not only will edits be lost at a given update, some changes you make on a whim may compromise other functionality or even endanger the security of your site.

    Action Hooks & Filters are included within the core to allow modifying functionality without modifying code.

    An example of using the login_redirect filter to redirect certain users can be found here and is a much more robust solution to your problem.

    For your specific problem, you want to do this:

    function login_redirect( $redirect_to, $request, $user ){
        return home_url('news.php');
    }
    add_filter( 'login_redirect', 'login_redirect', 10, 3 );
    
    0 讨论(0)
提交回复
热议问题