1

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?

3

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();
    }
}
  • Ok thanks. I used the first snippet and it works, only problem now is the menu item moved up one and is no longer the last item. Any way to fix this? – Rich Oct 9 '13 at 2:20
  • change 9 (theat was on your code...) with count($menu) so: $pos = count($menu); $menu[$pos] = array( ... – gmazzap Oct 9 '13 at 2:45
  • ok i did but its still in the same spot -- function register_web_menu_page () { global $menu; $pos = count($menu); $menu[$pos] = 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 ); } – Rich Oct 11 '13 at 23:24
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.