wp_redirect not working on admin menu page

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?

Solutions Collecting From Web of "wp_redirect not working on admin menu page"

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:

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();
    }
}

You can use load-(page) (reference)

From your source code.

add_action('admin_menu', 'register_web_menu_page');
function register_web_menu_page() {
    $hook = add_menu_page('View My Website', 'View My Website', 'add_users', 'web_menu_page', 'web_menu_page', '', null, 9); 
    add_action('load-' . $hook, 'redirectNiceUrl');
}

function redirectNiceUrl() {
    wp_redirect(home_url());
}