Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems adding action links to WordPress plugin?

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?

like image 399
Drew Angell Avatar asked Dec 15 '22 00:12

Drew Angell


2 Answers

As explained by Akshay, we need to use the plugin_basename as suffix for the hook. But for completeness, a couple of missing details.

  1. The hook can also take a prefix to show the action links in the Network screen of a Multisite installation:

    $basename = plugin_basename( __FILE__ );
    $prefix = is_network_admin() ? 'network_admin_' : '';
    add_filter( 
        "{$prefix}plugin_action_links_$basename", 
        array( $this,'plugin_action_links' ), 
        10, // priority
        4   // parameters
    );
    
  2. The hook takes 4 parameters, which may contain useful information for building the links:

    public function plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) 
    {
        // $plugin_file is the plugin_basename
        // $plugin_data contains the plugin's header information
        // $context is the current screen (all: All plugins, active: Active plugins)
    }
    

    If we use the hook without the basename suffix, we can use the $plugin_file param to filter out only our plugin(s).

like image 89
brasofilo Avatar answered Jan 02 '23 23:01

brasofilo


Use plugin_basename( __FILE__ ) instead of __FILE__.

Use following filter to add action links.

add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array($this, 'plugin_action_links') );

I had working this filter in one of my plugin, hope its work for you too.

like image 26
Akshay Paghdar Avatar answered Jan 02 '23 22:01

Akshay Paghdar