Skip to main content
1 of 2
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

Problem is that the function you use run after http header are sent, so it can't redirect.

You have to use another way.

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();
    }
}
gmazzap
  • 46.3k
  • 6
  • 95
  • 147