Add custom Action and Meta links for your WordPress plugin

By default, WordPress plugins come with “action” and “meta” links on the Plugins administration page. The action links are on the left of each row, e.g. De/activate or Edit, whereas the meta links are on the right side, e.g. Visit Site or the author links.

As a plugin developer, you’ll often want to add quick-links directly to settings or administration pages to make it easier for your users to find them. Thanks to a great writeup by Themato Soup, I was able to simplify this somewhat obscure and often-abused process with a helper class. You can Download plugin_admin_links.class.php, or continue to follow along.

Essentially, you’ll want to:

  1. include the helper file and instantiate the Plugin_Admin_Links class to a variable.
  2. after adding pages with add_menu_page (and variants), use the instantiated helper to add links with a label and target url.

Usage:

// add your pages
add_action('admin_menu', 'add_my_pages');

function add_my_pages() {
	$my_plugin_links = new Plugin_Admin_Links(__FILE__);

	// add your pages (omitted)
	$my_pages['help'] = add_submenu_page('...');

	// now using menu slug from previous step, add your custom link
	$my_plugin_links->set_link('action', 'help', 'Help', $my_pages['help']);
}

That’s it. No muss, no fuss. The first highlighted line can be called anywhere, whether that be inside an admin_init hook or just in your plugin file. The important trick is that $my_plugin_links needs to be available later inside your page adding callback. So it makes the most sense to just include it in the same place you’re adding pages.

Then, once you’ve registered a page (and have its WP-generated id), you can set a link for that page using the helper, providing:

‘action’
the facet for the link — either as an 'action' or 'meta' link
‘help’
the link identifier — I’m not actually sure this is important, but it’ll be the link array key, so theoretically you could use it to overwrite existing links.
‘Help’
the link label – the text the user will see
$my_pages['help']
the WP-generated page id — the class should be able to autoparse it into an actual page url, but if not see below.

You can also directly specify the HTML for the link, so that you could enter branding messaging or external donate links, or smiley faces, whatever, using:

// manually specify the HTML
$my_plugin_links->set_link_html($facet, $key, $html);

Here’s the full class for reference:

<?php
/**
 * Add admin links for a plugin.
 * Part of the "Plugin-Shared-Library" plugin http://wordpress.org/extend/plugins/plugin-shared-library/
 * @author zaus (http://drzaus.com)
 * @license none - use at-will, no guarantees; please keep this metadata intact with author reference
 */
class Plugin_Admin_Links {

	/**
	 * The list of links to add
	 */
	private $links = array('action'=>array(), 'meta'=>array());

	/**
	 * The plugin basename, as determined by constructor passing in calling file with __FILE__
	 */
	private $basename;

	/**
	 * Add necessary hooks to register plugin links
	 * @param string $calling_file use __FILE__ to get the correct plugin name automatically
	 * @param string $meta_prefix {default ''} use to specify adding meta row to network admin or standard
	 */
	public function __construct($calling_file, $meta_prefix = '') {
		$this->basename = plugin_basename( $calling_file );
		// add hook for plugin links
		add_filter( 'plugin_row_meta', array(&$this, 'set_plugin_meta'), 10, 2 );
		add_filter($meta_prefix . 'plugin_action_links_' . $this->basename, array(&$this, 'set_plugin_links'), 10, 2 );

	}
	/**
	 * PRIVATE: Add link to on plugin page (DO NOT USE EXPLICITLY)
	 * @source 
	 * @source http://bavotasan.com/2009/a-settings-link-for-your-wordpress-plugins/
	 */
	public function set_plugin_meta($links, $file) {
		
		// create link
		if ($file == $this->basename) {
			//array_unshift($links, $settings_link); 
			return array_merge( $links, $this->links['meta'] );//$this->set_plugin_links($links);
		}
		return $links;
	}

	/**
	 * PRIVATE: Add settings link on plugin page (DO NOT USE EXPLICITLY)
	 * @source http://thematosoup.com/development/add-action-meta-links-wordpress-plugins/
	 * @source http://bavotasan.com/2009/a-settings-link-for-your-wordpress-plugins/
	 */
	public function set_plugin_links($links) {

		//array_unshift($links, $this->links); 
		return array_merge( $links, $this->links['action'] );
	}

	/**
	 * Set the raw HTML for the given link
	 * @param string $facet {meta, action} where to add the link
	 * @param string $key the link list key
	 */
	public function set_link_html($facet, $key, $html){
		$this->links['meta'==$facet ? 'meta' : 'action'][$key] = $html;
	}

	/**
	 * Set the link to the default format from a WP page id
	 * @param string $facet {meta, action} where to add the link
	 * @param string $key the link list key
	 * @param string $label the display label
	 * @param string $page_id the page id as given from <code>add_menu_page</code>
	 */
	public function set_link($facet, $key, $label, $page_id) {
		$this->set_link_html($facet, $key
			, sprintf( '<a href="%s">%s</a>'
				, str_replace('_page_', '.php?page=', $page_id)
				, __($label)
				)
			);
	}//--	fn	set_link
}///---	class	Plugin_Admin_Links

Note:
Wordpress changed how you add action and meta links between versions 2.7 and 2.8 — this solution should work for both old and new, although you might not be able to add either action or meta, I’m not sure.

One thought on “Add custom Action and Meta links for your WordPress plugin

  1. Thanks for mentioning our post :)

    I agree, this topic surprisingly was not documented well at all on the internet, that’s why I did that post in the first place. Great idea to expand it to a helper class.

Leave a Reply

Your email address will not be published. Required fields are marked *