parent_file
云策文档标注
概述
parent_file 过滤器用于修改 WordPress 后台管理菜单中子菜单项的父文件,允许插件动态调整子菜单的归属位置,从而控制菜单高亮状态。
关键要点
- 过滤器名称:parent_file
- 主要用途:允许插件移动子菜单项,以改变其父菜单的标识
- 参数:$parent_file(字符串类型),表示当前的父文件
- 引入版本:WordPress MU 3.0.0
- 调用方式:通过 apply_filters('parent_file', $parent_file) 应用
代码示例
/**
* 设置隐藏子菜单的父文件,以显示父菜单为活动状态。
*
* @param $parent_file
*
* @return mixed
*/
function wporg_func_parent_file_callback( $parent_file ) {
global $plugin_page;
if ( 'my-plugin-custom-child' === $plugin_page ) {
$plugin_page = 'my-plugin-custom-parent';
}
return $parent_file;
}
add_filter( 'parent_file', 'wporg_func_parent_file_callback' );注意事项
- 示例代码通过修改全局变量 $plugin_page 来调整父文件,但需谨慎使用,可能不够健壮并引发潜在问题。
- 建议在实际开发中评估替代方案,确保兼容性和稳定性。
原文内容
Filters the parent file of an admin menu sub-menu item.
Description
Allows plugins to move sub-menu items around.
Parameters
$parent_filestring-
The parent file.
Source
$parent_file = apply_filters( 'parent_file', $parent_file );
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |
Skip to note 2 content
Adarsh Verma
Example:
/** * Sets the parent file for hidden sub menus. * Showing the parent menu active. * * @param $parent_file * * @return mixed */ function wporg_func_parent_file_callback( $parent_file ) { global $plugin_page; if ( 'my-plugin-custom-child' === $plugin_page ) { $plugin_page = 'my-plugin-custom-parent'; } return $parent_file; } add_filter( 'parent_file', 'wporg_func_parent_file_callback' );$plugin_pageto the parent slug. My concern is that it’s not the most robust approach and could cause issues down the line. I could be wrong though.