我正在尝试添加一些动作链接到WordPress插件.我从以下开始.
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 );
}
}
Run Code Online (Sandbox Code Playgroud)
这样做除了它将链接放在当前启用的每个插件上而不是我自己的插件上.我正在查看关于此的WordPress codex信息,它显示使用附加到过滤器名称的文件名.所以我做了这样的调整:
add_filter('plugin_action_links_'.__FILE__, array($this,'plugin_action_links'));
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,所有链接都会完全消失,并且它们不会出现在任何地方,甚至不是我自己的.我在这做错了什么?
正如Akshay所解释的那样,我们需要使用plugin_basenameas后缀作为钩子.但为了完整性,还有一些遗漏的细节.
钩子还可以使用前缀来显示多站点安装的网络屏幕中的操作链接:
$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
);
Run Code Online (Sandbox Code Playgroud)钩子有4个参数,可能包含构建链接的有用信息:
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)
}
Run Code Online (Sandbox Code Playgroud)
如果我们使用没有basename后缀的钩子,我们可以使用$plugin_fileparam来过滤掉我们的插件.