Redirect "Sorry, you are not allowed to access this page." to Home
how to redirect the error message page "Sorry, you are not allowed to access this page." to the Home page?
how to redirect the error message page "Sorry, you are not allowed to access this page." to the Home page?
Accroding to the file "wp-content/languages/admin-[lang].po" and an additional search inside of a local copy of WP 5.6.1 the phrase is used 17 times. Not in all contexts there is an obvious hook to catch the error screen but in a lot of cases the error screen appears when a user wants to access an area of the backend where he has no appropriate permissions (eg role "editor" in "wp-admin/plugins.php").
If this is the case the error screen is triggered in "wp-admin/includes/menu.php" and luckily sinve WP 2.5.0 there is a hook right before the triggering wp_die
-function, which is do_action('admin_page_access_denied')
.
So in this case one could redirect the error screen like so.
do_action( 'admin_page_access_denied', function()
{
die( // make shure execution dies right after redirect
wp_redirect( // redirect
site_url() // to the home page
)
);
// short: die( wp_redirect( site_url() ) );
} );
#: wp-admin/includes/menu.php:350 wp-admin/my-sites.php:17
#: wp-admin/network/site-info.php:32 wp-admin/network/user-new.php:37
#: wp-admin/network/index.php:17 wp-admin/network/site-users.php:50
#: wp-admin/network/upgrade.php:38 wp-admin/network/users.php:14
#: wp-admin/network/users.php:24 wp-admin/network/users.php:46
#: wp-admin/network/users.php:60 wp-admin/network/users.php:150
#: wp-admin/network/site-themes.php:57 wp-admin/network/settings.php:17
#: wp-admin/network/site-settings.php:32 wp-admin/network/sites.php:14
#: wp-admin/network/sites.php:140
source: "wp-content/languages/admin-[lang].po"
I think that is a function of the theme, which will return that message under whatever conditions it defined. (Hard to say exactly what file of the theme, since you didn't specify the theme name. But a search for that phrase should find the file.)
If I am correct, then you should (best practice) make a child theme, then copy the theme's PHP file that displays that message into the child theme folder. Then edit the file in the child theme folder to change the part of the code that displays the message to a wp_redirect()
(see https://developer.wordpress.org/reference/functions/wp_redirect/ ) . Make sure you include the exit
command after the wp_direct()
.
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.