Problem is that the function you use run after http header are sent, so it can't redirect.
You have to use another way.
SetupOne method can be intercept the global menu
variable and add a new menu item with all properties:
add_action( 'admin_menu', 'register_web_menu_page', 999);
function register_web_menu_page () {
global $menu;
$menu[9] = array (
'View My Website', // menu title
'add_users', // capability
home_url(), // menu item url
null,
'menu-top menu-icon-generic toplevel_page_web_menu_page', // menu item class
'View My Website', // page title
false // menu function
);
}
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):
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', '__return_false', '', null, 9);
}
add_action('admin_init', 'redirect_to_site', 1);
function redirect_to_site() {
if ( isset($_GET['page']) && $_GET['page'] == 'web_menu_page' ) {
wp_redirect( home_url() );
exit();
}
}