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).
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:
-
I chose to use the
switch
/case
statement in theafter_setup_theme
hook /redirect_from_admin_menu()
function so that it would be easier to add additionalgoto
redirects if you need to; just add more case statements. -
the
wp_redirect()
andwp_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!