Im trying to create a admin menu link on the backend dashboard, that will take users to the front end of their site. Im trying to use this code, but for some reason the redirect is not working.
// Custom Menus
add_action('admin_menu', 'register_web_menu_page');
function register_web_menu_page() {
add_menu_page('View My Website', 'View My Website', 'add_users', 'web_menu_page', 'web_menu_page', '', null, 9);
}
function web_menu_page(){
wp_redirect( home_url() );
exit;
}
The user clicks the menu link and are take to the web_menu_page and are not being redirected to the home url.
Im not sure why it isnt redirecting, any help?
Problem is that the function you use run after http header are sent, so it can’t redirect.
You have to use another way.
One method can be intercept the global
menu
variable and add a new menu item with all properties:Thisis not exaclty a standard way, because you know standard way to add menu items is to use
add_menu_page
function.If you want to use only standard practices, setup the menu using doing-nothing function, just like
'__return_false'
, and then use another function to redirect to home page if the$_GET['page']
is = to your menu slug on admin init (before headers are sent):You can use
load-(page)
(reference)From your source code.