Adding a Link to the WordPress Dashboard Menu that opens an Internal or External Page. [Updated]

 
"
Easily add a Custom Link to your Dashboard Menu…
 

Dashboard Menu LinkUpdated: January 23, 2012 – This tip walks you through adding a Statistics link for Google Analytics within the WordPress Admin under the Dashboard tab. Then how to add multiple links to the Dashboard menu and finally how to open an internal page from the Dashboard menu.

 

Sources:

Adding a Link to the Dashboard of WordPress

The example below is two functions. The second function (dashboardMenu) adds the link within the dashboard, then calls the first function (linkedPage).

What To Do…

  • On Line 8 replace the ENTER-YOUR-GOOGLE-ANALYTICS-URL-HERE with your Google Analytics URL or any URL you wish to open.
  • On Line 10 replace YOUR-DOMAIN with your domain.

/* The Redirect Function */
function linkedPage(){
	if(!current_user_can('manage_options')){
		wp_die( __('You do not have sufficient permissions to access this page.'));
	}
?>
<script type="text/javascript">
	window.open("ENTER-YOUR-GOOGLE-ANALYTICS-URL-HERE"); /* Update This Line */
	function delay(){
		window.location.href = "http://YOUR-DOMAIN.com/wp-admin/index.php"; /* Update This Line */
	}
</script>
<body onLoad="setTimeout('delay()', 4000)">
<? } /* end function */
/* Add Dashboard Link */
function dashboardMenu(){
	if(function_exists('add_submenu_page'))
	add_submenu_page('index.php', __('Statistics'), __('Statistics'), 'manage_options', 'stats', 'linkedPage');
} /* end function */
add_action('admin_menu', 'dashboardMenu');

How Does It Work?

When the Statistics link is clicked in the dashboard, the linkedPage function is called. This sets two Javascripts in action, the first opens Google Analytics in a tab-less browser window. The second redirect waits a few seconds, then returns back to your WordPress Admin homepage.

Adding Multiple Dashboard Links

Duplicate the linkedPage function and rename it, and adjust the urls within the Javascript. Then in the dashboardMenu function, duplicate the add_submenu_page line, adjust the link name and finally replace the linkedPage function with the new function name.


/* Multiple Redirects */
function secondFunctionName(){
	if(!current_user_can('manage_options')){
		wp_die( __('You do not have sufficient permissions to access this page.'));
	}
?>
<script type="text/javascript">
	window.open("OUT-GOING-URL"); /* Update This Line */
	function delay(){
		window.location.href = "http://YOUR-DOMAIN.com/wp-admin/index.php"; /* Update This Line */
	}
</script>
<body onLoad="setTimeout('delay()', 4000)">
<? } /* end function */
/* Add Dashboard Link */
function dashboardMenu(){
	if(function_exists('add_submenu_page'))
	add_submenu_page('index.php', __('Statistics'), __('Statistics'), 'manage_options', 'stats', 'linkedPage');
	add_submenu_page('index.php', __('New Link'), __('New Link'), 'manage_options', 'link', 'secondFunctionName');
} /* end function */
add_action('admin_menu', 'dashboardMenu');

Opening an Internal Page

Instead of redirecting to an outsideURL, you can open a page directly within the admin, then add all the links and goodies you like to that page.

The example below offers two methods for display HTML. The first is to include the html within the function – a much cleaner way of doing it, and the second is to display the html directly within the function.


function linkedPage(){
	if(!current_user_can('manage_options')){
		wp_die( __('You do not have sufficient permissions to access this page.'));
	}
/* Method #1 - Include the HTML */
//include("full/path/to/file.php");
/* */
/* Method #2 - HTML within the Function */ ?>
<ul>
	<li><a href="#" target="_blank">Bird is the Word</a></li>
</ul>
<? }
function dashboardMenu(){
	if(function_exists('add_submenu_page'))
	add_submenu_page('index.php', __('My Page'), __('My Page'), 'manage_options', 'mypage', 'linkedPage');
}
add_action('admin_menu', 'dashboardMenu');

Simple Enough!

This can save you a bit of time and it can be twisted in many various ways. Add links to Feed Stats, Online Tools, Related Sites, Social Networks and whatever else you like.


Have a comment? Find an error? Would you like to improve on this article?

Then shoot me some feedback! ~tribalNerd