admin管理员组

文章数量:1335179

I am creating a plugin to redirect all sign in attempts to a dedicated page rather than using the builting wordpress page
so i created this hook action for the admin_init with the highest priority over other callbacks

add_action( 'admin_init', 'redirect_callback', 1)

the callback function will then make sure the user is not signed in already to make the redirect

function redirect_callback() {
    if ( ! is_user_logged_in() ) {
      wp_redirect( /* my link */ );
      exit;
    }
}

and of course the is_user_logged_in() needs this dependency

include(ABSPATH."wp-includes/pluggable.php")

visiting this link: url/wp-admin still doesn't redirect anywhere, it still pulls up the wordpress press sign in page.
anyone can help ?

I am creating a plugin to redirect all sign in attempts to a dedicated page rather than using the builting wordpress page
so i created this hook action for the admin_init with the highest priority over other callbacks

add_action( 'admin_init', 'redirect_callback', 1)

the callback function will then make sure the user is not signed in already to make the redirect

function redirect_callback() {
    if ( ! is_user_logged_in() ) {
      wp_redirect( /* my link */ );
      exit;
    }
}

and of course the is_user_logged_in() needs this dependency

include(ABSPATH."wp-includes/pluggable.php")

visiting this link: url/wp-admin still doesn't redirect anywhere, it still pulls up the wordpress press sign in page.
anyone can help ?

Share Improve this question asked Jun 1, 2020 at 13:37 user101018user101018 2
  • I think the template_redirect hook (developer.wordpress/reference/hooks/template_redirect) should fit better to what you try to accomplish. – Dacobah Commented Jun 1, 2020 at 17:45
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Jun 1, 2020 at 18:54
Add a comment  | 

2 Answers 2

Reset to default 0

this line did the task perfectly

add_filter( 'login_url', function(){return /* the custom URL */;});

here is the explanation:(valid for wordpress 5.4.1)
by the time admin_init fires, it's too late already.
once url/wp-admin is visited, index.php launches first
then requires admin.php (line 10)
then the file will execute 94. auth_redirect();
before 170. do_action( 'admin_init' );

auth_redirect() core function uses wp_login_url()
which in its turn uses login_url, as indicated in the wordpress documentation plage. we can apply a filter to it with any URL we like.

So I add an action to call the function:

// Redirect anyone from the wp-login screen to the custom distributor login

add_action('init','custom_login');

Then I create a function to direct to custom url:

function custom_login(){  
   global $pagenow;  
   if( 'wp-login.php' == $pagenow && $_GET['action']!="logout") {  
     wp_redirect('https://example/new_login/');  
   }  
}  

本文标签: wp adminredirect to a dedicated sign in page