Skip to content

fix: prevent clean_nav_menu crash on PHP 8.5 and reduce theme breakage#22

Merged
itsrafsanjani merged 1 commit into
developfrom
fix/clean-nav-menu-php85-compat
Jun 15, 2026
Merged

fix: prevent clean_nav_menu crash on PHP 8.5 and reduce theme breakage#22
itsrafsanjani merged 1 commit into
developfrom
fix/clean-nav-menu-php85-compat

Conversation

@itsrafsanjani

@itsrafsanjani itsrafsanjani commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

The Clean Navigation Menu optimization (includes/Optimizations/General.php) caused two problems, both addressed here:

  1. Fatal crash on PHP 8.5 — the nav_menu_css_class callback called array_intersect( $classes, [...] ) assuming $classes is always an array. Some page builders pass a string, which throws an uncaught TypeError on PHP 8.5+ and white-screens the entire frontend (HTTP 500).
  2. Theme/plugin breakage — the feature allowlisted only 4 classes (stripping everything else) and removed all menu item IDs, breaking submenus, mega menus, JS targeting and accessibility on themes that rely on custom menu classes. It was also enabled by default.

Changes

  • General.php — add an is_array() guard to the nav_menu_css_class callback (returns the value unchanged when not an array); this matches the existing emoji guard at General.php:48.
  • General.php — remove add_filter( 'nav_menu_item_id', '__return_null' ) so menu item IDs are preserved.
  • Optimizations.php — flip the clean_nav_menu default from truefalse, making it opt-in. Existing sites keep their saved value (the settings save handler always writes every field; defaults only apply when no option row exists).

Note: the class allowlist contents are unchanged — when a user explicitly enables the feature, it behaves as before, just crash-safe and without stripping IDs.

Testing

  • php -l passes on both files.
  • String passed to nav_menu_css_class returns unchanged (no TypeError).
  • Array input still trims to the allowlisted classes.
  • Menu item IDs present in rendered markup.
  • Fresh install resolves clean_nav_menu to false; previously-saved enabled sites stay enabled.

Summary by CodeRabbit

  • Bug Fixes

    • Improved navigation menu CSS class handling to properly validate input types before processing, ensuring more robust behavior.
  • Chores

    • Updated default configuration settings for the navigation menu optimization feature to be disabled by default.

…akage

The clean_nav_menu optimization called array_intersect() on the
nav_menu_css_class filter assuming $classes is always an array. Some
page builders pass a string, which fatals with a TypeError on PHP 8.5+
and white-screens the whole frontend.

- Guard the filter callback with is_array(), returning the value
  unchanged when it is not an array.
- Stop forcing nav_menu_item_id to null so menu item IDs are preserved
  for JS/anchor/accessibility targeting.
- Make the optimization opt-in (default false) so it no longer breaks
  themes that rely on custom menu classes out of the box. Existing
  sites keep their saved setting.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 20ae498b-9e78-416e-ac01-11f25f041330

📥 Commits

Reviewing files that changed from the base of the PR and between 23ff681 and 294d950.

📒 Files selected for processing (2)
  • includes/Optimizations.php
  • includes/Optimizations/General.php

Walkthrough

The clean_nav_menu default in get_options() is flipped from true to false. Inside clean_nav_menu(), the nav_menu_css_class filter callback gains an early return that passes $classes through unchanged when it is not an array, before the existing array_intersect logic runs.

Changes

clean_nav_menu Safety Fix and Default Change

Layer / File(s) Summary
Type guard in filter callback and default disabled
includes/Optimizations/General.php, includes/Optimizations.php
nav_menu_css_class filter returns $classes as-is when not an array; clean_nav_menu default changed from true to false in get_options().

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐇 A menu of classes came tangled one day,
Not always an array, to my dismay!
A quick little guard now checks at the door,
And false is the default — opt in, not before.
Hippity-hop, the nav stays neat and right! 🥕

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/clean-nav-menu-php85-compat

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@itsrafsanjani itsrafsanjani merged commit 61f85d7 into develop Jun 15, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant