WordPress Plugin Settings Link
by Riley MacDonald, August 15, 2013

Several WordPress plugins I’ve seen have a “Settings” or “Donate” link before the “Deactivate” and “Edit” links in the WordPress plugins menu. This post will outline how to add this to your plugin.

WP Plugin Settings

WP Plugin Settings


To add a settings menu/page for your plugin use the add_action( ‘admin_menu’ ) WordPress function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Earlier in my plugin I defined $plugin_slug
protected $plugin_slug = 'plugin-name';
//Register the plugin page
add_action( 'admin_menu', 'your_plugin_admin__menu_function' );
//Add the page
public function your_plugin_admin__menu_function() {
  add_plugins_page(
    'Your Plugin Name Settings', //The text to be displayed in the title tags of the page
    'Your Plugin Name',          //The text to be used for the menu
    'manage_options',            //The capability required for this menu to be displayed
    $plugin_slug,                //The slug name
    'get_admin_menu'             //The function called to output the content/page
  );
}
//Get the admin menu page (or output it here)
public function get_admin_menu() {
  include( 'views/admin.php' );
}

This successfully adds a sub menu to the WordPress plugin menu, but I wanted the link directly next to the plugin itself. The solution involved adding a new item to the $links array using the plugin_action_links_{plugin-file.php} filter:

1
2
3
4
5
6
7
8
9
10
11
//Add the filter with your plugin information
add_filter( 'plugin_action_links_' . plugin_basename( plugin_dir_path( __FILE__ ) . $plugin_slug . '.php'), 'plugin_action_links' );
//The callback function to add the settings link
public function plugin_action_links( $links ) {
  return array_merge(
    array(
      'settings' => '<a href="' . admin_url( 'plugins.php?page=' . $plugin_slug ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>'
    ),
    $links
  );
}
Open the comment form

Leave a comment:

Comments will be reviewed before they are posted.

User Comments:

velcro.co.uk on 2017-08-29 12:52:18 said:
Some truly prime posts on this website , saved to fav.