1
votes

i am facing issue login redirection issue, on my site when ever i tried to login through wp-admin or wp-login.php with admin details. website redirects me to the home page it should be on wordpress dashboard. "this how i do " i access to https://www.exemple.com/wp-login.php?redirect_to=https%3A%2F%2Fwww.www.exemple.com%2Fwp-admin%2F&reauth=1" when i log in with admin details" redirect me to https://www.exemple.com when i face this problem i tried to look for online solution then i find some ways but aren't working for me" exemple of thing that i did?

disable theme and plugin remove htaccess file change the databases to other user install update of wordpress manual via ftp client i add new user from database to my wordpress but is not accessing. also i tried to upgarade php from 7.0 to 7.4 but the site goes down

Please help.

thank in advance for your help

1

1 Answers

0
votes

You're looking for the login_redirect hook filter.

Our train of thought is the following: We want to make sure the $user is an object and is WP_User object (not a WP_error object).

We also want to check the user capabilities to determine if it is indeed an Administrator.

Upon result, we redirect.


Using WP_User::has_cap

While checking against a role in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

Head over @ https://wordpress.org/support/article/roles-and-capabilities/ to see all users default roles & capabilities.

Here I've chosen to use update_core which is only available to Super-Admin and Administrator roles.

You could choose a lesser capability to englobe multiple roles like delete_posts which would englobe Super-Admin, Administrator, and Editor roles. See @ Capability vs. Role Table.


Our final script

<?php
/**
* apply_filters
* @link https://developer.wordpress.org/reference/hooks/login_redirect/
* Filters the login redirect URL.
* @param String $redirect_to The redirect destination URL.
* @param String $requested_redirect_to The requested redirect destination URL passed as a parameter.
* @param Object $user WP_User object if login was successful, WP_Error object otherwise.
*/
add_filter( 'login_redirect', function ( $redirect_to, $requested_redirect_to, $user ) {

  if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) && $user->has_cap( 'update_core' ) )

    return $redirect_to = esc_url( home_url() );

}, 10, 3 ); ?>

This goes in your function.php file.