Customize Plugin Action Links
Programming and customizing your own WordPress plugins can be super fun and extremely satisfying. Yet sometimes the unique features, finding out to make them work, I find, is frustrating. Lots of blogs, none of which fully explain each variable and why things need to be done in the way they need to be done.
Hopefully this outline will help you, in this adaption feature.
THE CODE
add_filter( ‘plugin_action_links’, ‘my_function’, 10, 2 );
function my_function ( $actions, $file )
{
$this_plugin = plugin_basename(__FILE__);
if ($file == “The primary file of your plugin“)
{
$mylinks = array(‘<a href=”‘ . admin_url( ‘admin.php?page=your plugin page name’ ) . ‘”>Settings</a>’);
$actions = array_merge( $actions, $mylinks );
}
return $actions;
}
Seems simple enough, yet there are reason and sources to make the code work.
1. From the wordpress plugin folder we have:
/public_html/wp-content/plugins/ –> The wordpress plugin folder
2. Within this folder we have your plugin folder name
/public_html/wp-content/plugins/YourPluginFolderName –> Folder plus file name
3. Withing your plugin folder, what is the primary file name? In this example, it is main.php
4. So the The “primary file of your plugin” is equal to:
The primary file of your plugin = YourPluginFolderName/main.php
We now change the previous code to as follows:
THE CODE
add_filter( ‘plugin_action_links’, ‘my_function’, 10, 2 );
function my_function ( $actions, $file )
{
$this_plugin = plugin_basename(__FILE__);
if ($file == “YourPluginFolderName/main.php“)
{
$mylinks = array(‘<a href=”‘ . admin_url( ‘admin.php?page=your plugin page name’ ) . ‘”>Settings</a>’);
$actions = array_merge( $actions, $mylinks );
}
return $actions;
}
But you may ask, why the IF statement equal to the passed in file name. That is because EVERY SINGLE PLUGIN IN YOUR SYSTEM will be passed into this function, regardless if the filer is only in your own plugin. So we need to limit which plugins have the new link assigned.
PART 2 CHANGE
The final variable is the actual admin page that you want to refer the user to use. This could also be external pages, but in our example we are giving a direct link to the settings of the plugin.
If you look in the image below, you will find this link: admin.php?page=Setting_page_slug
This is the sample page link, that you refer to in your add_submenu_page definitions.
Now subsitute this link in the code as follows:
THE FINAL CODE
add_filter( ‘plugin_action_links’, ‘my_function’, 10, 2 );
function my_function ( $actions, $file )
{
$this_plugin = plugin_basename(__FILE__);
if ($file == “YourPluginFolderName/main.php”)
{
$mylinks = array(‘<a href=”‘ . admin_url( ‘admin.php?page=Setting_page_slug‘ ) . ‘”>Settings</a>’);
$actions = array_merge( $actions, $mylinks );
}
return $actions;
}
That is it!
The Author
CEO & Technical Director of Multimedia Designs, LLC, System Designer, Developer, Producer with 15+ years in the internet design and development space and 25+ years in the Software Development for the entertainment industry.
Winner of the prestigious International Themed Entertainment Association award for interactive design. She holds a degree in Computer Engineering & Mathematics from California State University, Long Beach.