_wp_menus_changed()
云策文档标注
概述
_wp_menus_changed() 是一个 WordPress 内部函数,用于在主题切换后处理导航菜单配置的更新。它通过比较新旧主题的菜单位置映射,确保菜单设置正确迁移。
关键要点
- 函数在主题切换时自动调用,处理菜单位置的重映射。
- 使用 wp_map_nav_menu_locations() 来映射新旧菜单位置。
- 更新主题修改值 set_theme_mod() 以保存新菜单位置。
- 删除临时存储的旧菜单位置选项 delete_option('theme_switch_menu_locations')。
- 自 WordPress 4.9.0 版本引入。
代码示例
function _wp_menus_changed() {
$old_nav_menu_locations = get_option( 'theme_switch_menu_locations', array() );
$new_nav_menu_locations = get_nav_menu_locations();
$mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations );
set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations );
delete_option( 'theme_switch_menu_locations' );
}注意事项
- 这是一个内部函数,通常不应直接调用,由 WordPress 核心在主题切换时自动处理。
- 依赖多个辅助函数如 get_option()、get_nav_menu_locations() 等来获取和操作数据。
- 确保菜单位置映射逻辑正确,以避免在主题切换后菜单丢失或错位。
原文内容
Handles menu config after theme change.
Source
function _wp_menus_changed() {
$old_nav_menu_locations = get_option( 'theme_switch_menu_locations', array() );
$new_nav_menu_locations = get_nav_menu_locations();
$mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations );
set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations );
delete_option( 'theme_switch_menu_locations' );
}
Changelog
| Version | Description |
|---|---|
| 4.9.0 | Introduced. |