Are you looking for a way to redirect admin pages to any location? While there’s probably a plugin for this, we have created a quick code snippet that you can use to redirect admin pages to any location in WordPress.
The following snippet will let you redirect any of the admin pages to any location you wish based on the user capability. Another option would be to replace wp_redirect
with wp_die("some custom message");
to display a message instead of redirecting.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function wpsnipp_admin_pages_redirect() { global $pagenow; $admin_pages = array( 'edit-tags.php', 'edit-tags.php', 'link-manager.php', 'options-writing.php', 'options-reading.php', 'options-discussion.php', 'options-media.php', 'options-privacy.php', 'options-permalink.php', ); if(in_array($pagenow, $admin_pages)){ wp_redirect( admin_url('/') ); exit; } } if(!current_user_can('edit_post')){ add_action('admin_init', 'wpsnipp_admin_pages_redirect'); }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 15 best content marketing tools and how to create a contact form in WordPress.
What if you want to redirect a url like this? admin.php?page=account_page
You may have to check the value of $_GET[‘page’] as well.
You can’t use current_user_can() when in the global scope, within a plugin because it will cause Fatal error: Call to undefined function wp_get_current_user().