ClassicPress Plugin Development: Create a Plugin Action Link

ClassicPress PluginsThis post is part of the ClassicPress Plugin Development series in which I am going to look at both best practice for developing plugins and how I approach some requirements as well as some of the functions I commonly use.

When developing a plugin with an settings page, it is quite common to add a link to the settings page on the Plugins page; these links are known as plugin action links. This is an example from my URL Shortener plugin:

URL Shortener action link

The plugin action link can be added by using the add_filter function:

add_filter(string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1)

The $tag to use is plugin_action_links, the $function_to_add is the function you need to write to add the link and the accepted_args is 2

Below is an example of the filter using the plugin_action_links tag to add the link to the URL Shortener plugin’s settings page:

add_filter('plugin_action_links', 'azrcrv_urls_add_plugin_action_link', 10, 2);

The azrcrv_urls_add_plugin_action_link function called by the filter is:

/**
 * Add URL Shortener action link on plugins page.
 *
 * @since 1.0.0
 *
 */
function azrcrv_urls_add_plugin_action_link($links, $file){
	static $this_plugin;

	if (!$this_plugin){
		$this_plugin = plugin_basename(__FILE__);
	}

	if ($file == $this_plugin){
		$settings_link = '<a href="'.admin_url('admin.php?page=azrcrv-urls').'">'.esc_html__('Settings' ,'url-shortener').'</a>';
		array_unshift($links, $settings_link);
	}

	return $links;
}

The highlighted section is the menu_slug for the settings page.

Click to show/hide the ClassicPress Plugin Development Series Index

What should we write about next?

If there is a topic which fits the typical ones of this site, which you would like to see me write about, please use the form, below, to submit your idea.

Your Name

Your Email

Suggested Topic

Suggestion Details

Leave a Reply

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