plugin_action_links_{$plugin_file}
云策文档标注
概述
plugin_action_links_{$plugin_file} 是一个 WordPress 过滤器钩子,用于修改插件列表页面中特定插件的操作链接。它允许开发者自定义插件旁边的链接,如添加设置或支持链接。
关键要点
- 这是一个动态钩子,{$plugin_file} 部分需替换为插件文件相对于 plugins 目录的路径。
- 参数包括 $actions(操作链接数组)、$plugin_file(插件文件路径)、$plugin_data(插件数据数组)和 $context(插件上下文)。
- 默认链接可能包括 'activate'、'deactivate' 和 'delete',多站点环境下可能包含 'network_active' 和 'network_only'。
- 钩子应用于插件页面上的链接列表,位于激活/停用链接旁边。
代码示例
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'add_action_links' );
function add_action_links ( $actions ) {
$mylinks = array(
'Settings',
);
$actions = array_merge( $actions, $mylinks );
return $actions;
}注意事项
- 如果过滤器不在主插件文件中添加,需要手动指定 $plugin_file 路径,例如 'wpdocs-plugin/wpdocs-plugin.php'。
- 在类文件中使用时,可以将回调函数作为数组传递,如 array( $this, 'function_name' )。
- 从 WordPress 4.9.0 开始,'Edit' 链接已从操作链接列表中移除。
原文内容
Filters the list of action links displayed for a specific plugin in the Plugins list table.
Description
The dynamic portion of the hook name, $plugin_file, refers to the path to the plugin file, relative to the plugins directory.
Parameters
$actionsstring[]-
An array of plugin action links. By default this can include
'activate','deactivate', and'delete'. With Multisite active this can also include'network_active'and'network_only'items. $plugin_filestring-
Path to the plugin file relative to the plugins directory.
$plugin_dataarray-
An array of plugin data. See get_plugin_data() and the ‘plugin_row_meta’ filter for the list of possible values.
$contextstring-
The plugin context. By default this can include
'all','active','inactive','recently_activated','upgrade','mustuse','dropins', and'search'.
Source
$actions = apply_filters( "plugin_action_links_{$plugin_file}", $actions, $plugin_file, $plugin_data, $context );


Skip to note 4 content
shossain571
// Link to settings page from plugins screen add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'add_action_links' ); function add_action_links ( $links ) { $mylinks = array( '<a href="' . admin_url( 'options-general.php?page=mysettings' ) . '">Settings</a>', ); return array_merge( $links, $mylinks ); }Skip to note 5 content
techtimo
$plugin_file = plugin_basename(__FILE__)That works great if you add the filter in the main plugin file.
If not that’s not the case, you need to specify it.
Let’s say you have the following directory structure:
wp-content/plugins/wpdocs-plugin/wpdocs-plugin.phpThe
$plugin_filethen needs to bewpdocs-plugin/wpdocs-plugin.phpSkip to note 6 content
Sagar Bagul
If you want to add this in class file then take below reference,
add_filter( 'plugin_action_links_' . $plugin_file_path, array( $this, 'wpdocs_custom_action_link' ) ); public function wpdocs_custom_action_link( $links ) { // Build URL. $url = add_query_arg( 'page', 'custom-settings', get_admin_url() . 'tools.php' ); // Create the link and escape . $setting_link = '<a href="' . esc_url( $url ) . '">' . __( 'Custom Settings', 'domain' ) . '</a>'; // Adds the link to the end of the array. array_push( $links, $setting_link ); return $links; }