旧Artisteer主题在WP6.6.2+PHP8.2下生成空菜单链接问题
恢复Artisteer生成主题菜单在WP6.6.2+PHP8.2下的问题
我正在恢复一个被入侵的网站,原主题由Artisteer v4.3.0.60745生成。在全新本地WordPress 6.6.2(无插件)+PHP 8.2.20环境中运行时,头部和侧边栏菜单无法加载标题与链接,仅生成无标题的空href:
<ul class="art-hmenu menu-6"> <li class="menu-item-"><a href="#"></a> </li> <li class="menu-item-"><a href="#"></a> </li> <li class="menu-item-"><a href="#"></a> </li> <li class="menu-item-"><a href="#"></a> </li> <li class="menu-item-"><a href="#"></a> </li> </ul>
调试显示sorted_menu_items包含正确数据,但传入theme_MenuItem类后生成的$items均为null。相关代码片段如下:
wp-content/themes/osp5/library/navigation.php 第35行代码
/* custom menu */ function theme_get_list_menu($args = array()) { global $wp_query; $menu_items = wp_get_nav_menu_items($args['menu']->term_id); if (empty($menu_items)) return ''; $home_page_id = (int) get_option('page_for_posts'); $queried_object = $wp_query->get_queried_object(); $queried_object_id = (int) $wp_query->queried_object_id; $active_ID = null; $IdToKey = array(); foreach ((array) $menu_items as $key => $menu_item) { $IdToKey[$menu_item->ID] = $key; if ($menu_item->object_id == $queried_object_id && ( (!empty($home_page_id) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) || ( 'post_type' == $menu_item->type && $wp_query->is_singular ) || ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax )) ) ) { $active_ID = $menu_item->ID; } elseif ('custom' == $menu_item->object) { if (theme_is_current_url($menu_item->url)) { $active_ID = $menu_item->ID; } } } $current_ID = $active_ID; while ($current_ID && isset($IdToKey[$current_ID])) { $activeIDs[] = $current_ID; $current_item = &$menu_items[$IdToKey[$current_ID]]; $current_item->classes[] = 'active'; $current_ID = $current_item->menu_item_parent; } $sorted_menu_items = array(); foreach ((array) $menu_items as $key => $menu_item) { $sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item); } $items = array(); foreach ($sorted_menu_items as $el) { $id = $el->db_id; $title = $el->title; $classes = empty($el->classes) ? array() : (array) $el->classes; $active = in_array('active', $classes); $items[] = new theme_MenuItem(array( 'id' => $id, 'active' => $active, 'attr' => array( 'title' => strip_tags(empty($el->attr_title) ? $title : $el->attr_title), 'target' => $el->target, 'rel' => $el->xfn, 'href' => $el->url, 'class' => join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $el)) ), 'title' => $title, 'parent' => $el->menu_item_parent )); } $walker = new theme_MenuWalker(); $items = apply_filters('wp_nav_menu_objects', $items, $args); $items = $walker->walk($items, $args); $items = apply_filters('wp_nav_menu_items', $items, $args); return apply_filters('wp_nav_menu', $items, $args); }
第246行的theme_MenuItem类代码
/* menu item */ class theme_MenuItem { var $id; var $active; var $parent; var $attr; var $title; function theme_MenuItem($args = '') { $args = wp_parse_args($args, array( 'id' => '', 'active' => false, 'parent' => 0, 'attr' => '', 'title' => '', ) ); $this->id = $args['id']; $this->active = $args['active']; $this->parent = $args['parent']; $this->attr = $args['attr']; $this->title = $args['title']; } function get_start($level) { $class = theme_get_array_value($this->attr, 'class', ''); $class = 'menu-item-' . $this->id . (strlen($class) > 0 ? ' ' : '') . $class; $this->attr['class'] = ($this->active ? 'active' : null); $title = apply_filters('the_title', $this->title, $this->id); if (theme_get_option('theme_menu_trim_title')) { $title = theme_trim_long_str(strip_tags($title), theme_get_option($level == 0 ? 'theme_menu_trim_len' : 'theme_submenu_trim_len')); } return str_repeat(" ", $level + 1) . '<li' . theme_prepare_attr(array('class' => $class)) . '>' . '<a' . theme_prepare_attr($this->attr) . '>' . $title . '</a>' . " "; } function get_end($level) { return str_repeat(" ", $level + 1) . '</li>' . " "; } }
开启WP_DEBUG后出现以下弃用警告:
Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated in /Applications/MAMP/htdocs/osprzemien/wp-includes/formatting.php on line 2476 Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /Applications/MAMP/htdocs/osprzemien/wp-includes/class-wp-hook.php on line 326
问题根源
- PHP 8.0+严格类型检查:原代码中传递null给字符串类型参数(如
trim()、str_contains())触发弃用警告,导致数据处理异常。 - theme_MenuItem构造函数类型冲突:
$args['attr']默认值为空字符串,但实际传入的是数组,导致属性赋值错误。 - 未处理WP API返回的空值:新版本WP中
wp_setup_nav_menu_item返回的对象属性可能为空,原代码未做兜底处理。
修复步骤
1. 修复theme_MenuItem构造函数
将'attr'默认值从空字符串改为数组,避免类型冲突:
function theme_MenuItem($args = '') { $args = wp_parse_args($args, array( 'id' => '', 'active' => false, 'parent' => 0, 'attr' => array(), // 修正为数组类型 'title' => '', ) ); $this->id = $args['id']; $this->active = $args['active']; $this->parent = $args['parent']; $this->attr = $args['attr']; $this->title = $args['title']; }
2. 给菜单属性添加空值兜底
在theme_get_list_menu函数中,使用空值合并运算符处理可能为空的属性:
foreach ($sorted_menu_items as $el) { $id = $el->db_id ?? ''; $title = $el->title ?? ''; $classes = empty($el->classes) ? array() : (array) $el->classes; $active = in_array('active', $classes); $items[] = new theme_MenuItem(array( 'id' => $id, 'active' => $active, 'attr' => array( 'title' => strip_tags(empty($el->attr_title) ? $title : ($el->attr_title ?? '')), 'target' => $el->target ?? '', 'rel' => $el->xfn ?? '', 'href' => $el->url ?? '#', 'class' => join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $el)) ?? '' ), 'title' => $title, 'parent' => $el->menu_item_parent ?? 0 )); }
3. 修复get_start方法中的空值问题
将null替换为空字符串,避免传递无效参数给字符串处理函数:
function get_start($level) { $item_id = $this->id ?? ''; $class = theme_get_array_value($this->attr, 'class', ''); $class = 'menu-item-' . $item_id . (strlen($class) > 0 ? ' ' : '') . $class; // 将null改为空字符串 $this->attr['class'] = ($this->active ? 'active' : ''); $title = apply_filters('the_title', $this->title ?? '', $item_id); if (theme_get_option('theme_menu_trim_title')) { $title = theme_trim_long_str(strip_tags($title), theme_get_option($level == 0 ? 'theme_menu_trim_len' : 'theme_submenu_trim_len')); } return str_repeat(" ", $level + 1) . '<li' . theme_prepare_attr(array('class' => $class)) . '>' . '<a' . theme_prepare_attr($this->attr) . '>' . $title . '</a>' . " "; }
4. 优化theme_prepare_attr函数(若存在)
确保该函数过滤空属性值,避免生成无效HTML:
function theme_prepare_attr($attrs) { $output = ''; foreach ($attrs as $key => $value) { if (is_string($value) && trim($value) !== '') { $output .= ' ' . esc_attr($key) . '="' . esc_attr($value) . '"'; } } return $output; }
内容的提问来源于stack exchange,提问作者krazykipa
相关产品推荐
相关产品推荐

