I’m trying to add some action links to a WordPress plugin. I started with the following.
class Angelleye_PayPal_WooCommerce
{
public function __construct()
{
add_filter('plugin_action_links', array($this,'plugin_action_links'));
}
public function plugin_action_links($actions)
{
$custom_actions = array(
'configure' => sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=wc-settings&tab=checkout' ), __( 'Configure', 'paypal-for-woocommerce' ) ),
'docs' => sprintf( '<a href="%s" target="_blank">%s</a>', 'http://docs.angelleye.com/paypal-for-woocommerce/', __( 'Docs', 'paypal-for-woocommerce' ) ),
'support' => sprintf( '<a href="%s" target="_blank">%s</a>', 'http://www.angelleye.com/contact-us/', __( 'Support', 'paypal-for-woocommerce' ) ),
'review' => sprintf( '<a href="%s" target="_blank">%s</a>', 'http://wordpress.org/support/view/plugin-reviews/paypal-for-woocommerce', __( 'Write a Review', 'paypal-for-woocommerce' ) ),
);
// add the links to the front of the actions list
return array_merge( $custom_actions, $actions );
}
}
This works except that it puts the links on every single plugin that’s currently enabled instead of just my own. I’m looking at the WordPress codex info about this, and it shows to use the filename appended to the filter name. So I made the adjustment like this:
add_filter('plugin_action_links_'.__FILE__, array($this,'plugin_action_links'));
When I do that, though, all of the links go away altogether and they don’t show up anywhere, not even my own. What am I doing wrong here?
As explained by Akshay, we need to use the
plugin_basename
as suffix for the hook. But for completeness, a couple of missing details.The hook can also take a prefix to show the action links in the Network screen of a Multisite installation:
The hook takes 4 parameters, which may contain useful information for building the links:
If we use the hook without the basename suffix, we can use the
$plugin_file
param to filter out only our plugin(s).Use
plugin_basename( __FILE__ )
instead of__FILE__
.Use following filter to add action links.
I had working this filter in one of my plugin, hope its work for you too.
use
&$this
instead of$this
,