Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to add a arbitrary hyperlink to the WordPress admin menu (I mean the menu on the left when you log into the admin dashboard)? For example, can one add a link to Google?

In my particular case, I'd like to add a styleguide page for a Wordpress theme I'm working on so I can show the user how different HTML elements are styled with the theme and to demo how to style various things nicely.

This is the code I have so far (note: it is incomplete): In functions.php I have added a new menu item in the Appearance section:

add_action('admin_menu', 'create_theme_style_page');

function create_theme_style_page() {
  add_theme_page('Theme Styleguide', 'Theme Styleguide', 'administrator', basename(__FILE__),'build_styleguide_page');
}

function build_styleguide_page() {
 echo "Not sure what goes here to redirect admin to a arbitrary url?";
}

In build_styleguide_page(), attempting to redirect with header() gives a error (Cannot modify header information).

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

Hi @Tom,

If I understand your question correctly you don't so much need to know how to add a link to the menu (it seems you already know that) but instead need to learn how to get your link to redirect correctly, right?

Redirecting to an External URL from an Admin Menu Item

If so what you need to do is to not use the menu item function but instead "hook" WordPress early enough such that it hasn't output anything except possibly HTTP headers. The earliest hook when calling /wp-admin/themes.php appears to be after_setup_theme and it appears to work well.

Use a "Menu Slug" So You Can Test for it in a Hook

But to get it to work we first need to modify your call to add_theme_page in the admin_menu hook / your create_theme_style_page() function. We dropped the fifth parameter (the function to call to implement the admin option) because we don't need it, and changed the fourth parameter (the "menu slug") to be themes.php?goto=build-styleguide-page.

Although we could have chosen almost literally anything for the fourth parameter, given we are going to redirect I routed to the same page (themes.php) as other appearance options for consistency. I also just arbitrarily came up with the name goto because WordPress doesn't use it and it seems to make sense for this.

add_action('admin_menu', 'create_theme_style_page');
function create_theme_style_page() {
  add_theme_page(
    'Theme Styleguide',
    'Theme Styleguide',
    'administrator',
    'themes.php?goto=build-styleguide-page'
  );
}

BTW, we got rid of your build_styleguide_page() function because we don't need it for this solution.

Redirect in the Earliest Hook for themes.php: after_setup_theme

As our last bit of code we implement our after_setup_theme hook in our redirect_from_admin_menu() function. We have it test to see if the current page is themes.php and to ensure a URL parameter of goto was passed on the URL. Then it tests the value of goto using a switch/case statement to see if it has a value of 'build-styleguide-page'; if so it redirects to your stated hypothetical e.g. Google otherwise we just redirect back to the admin dashboard:

add_action('after_setup_theme', 'redirect_from_admin_menu');
function redirect_from_admin_menu($value) {
  global $pagenow;
  if ($pagenow=='themes.php' && !empty($_GET['goto'])) {
    switch ($_GET['goto']) {
      case 'build-styleguide-page':
        wp_redirect("http://www.google.com");
        break;
      default:
        wp_safe_redirect('/wp-admin/');
        break;
    }
    exit;
  }
}

Notes:

  1. I chose to use the switch/case statement in the after_setup_theme hook / redirect_from_admin_menu() function so that it would be easier to add additional goto redirects if you need to; just add more case statements.

  2. the wp_redirect() and wp_safe_redirect() functions don't actually terminate; you need to explicit issue an exit statement to get WordPress to stop and not override your redirect.

Hope this helps!

share|improve this answer
1  
Your solution works and you taught me a few things along the way - many thanks. –  Tom Aug 31 '10 at 8:51
    
@Tom - Awesome, glad I could help! –  MikeSchinkel Aug 31 '10 at 9:10
add comment

If you add your item into the menu by adding it to the $submenu array directly you'll avoid the need to do the redirect and be able to use a complete offsite URL as the menu link (i do this myself).

add_action( 'admin_menu' , 'admin_menu_new_items' );
function admin_menu_new_items() {
    global $submenu;
    $submenu['index.php'][500] = array( 'Menu item name', 'manage_options' , 'http://example.com' ); 
}  

index.php represents the key/link of the parent menu, so in the example i'm basically adding an item to the Dashboard menu..

Sure, it's not quite as pretty as using the add_{type}_page functions, but it does work-around the fact WordPress prevents off-site URLs in admin menu links..

I use something similar to add my various virtual hosts into the menu, so i can switch to other installations directly from the admin menu (all done with a script, but basically uses a glorified version of the above code).

NOTE: If you decide to use the code, just be careful not to assign a key that's in use(the 500 array key in the example code).

TIP: You can actually use negative values in the menu keys to, so you can force you menu items to sit before any of the native WordPress items.

share|improve this answer
    
Thanks for the tip. –  Tom Nov 10 '10 at 10:22
    
You're welcome.. :) –  t31os Nov 10 '10 at 11:07
    
NOTE: I think you will need to set that negative key as a string though, ie. '-5' and not -5, else the key is regarded invalid(i think).. (negative strings works though, if i'm not mistaken).. –  t31os Dec 9 '10 at 23:52
1  
This particular answer helped me with my plugin Post Status Menu Items (you can probably guess what it does). Anyway, because I didn't know what other plugins would be adding stuff to what menus, I thought I'd just add that I used array_push() to append the menu items to avoid colliding with any other plugins. –  mrwweb Apr 28 '12 at 21:42
1  
+1. Rather than picking a key and hoping, just grab the last key and increment. For example, when adding a submenu item to the Tools menu: global $submenu; $key = end(array_keys($submenu['tools.php'])) + 1; $submenu['tools.php'][$key++] = etc. –  webaware May 29 '13 at 7:19
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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