Workaround to walk function no more compatible with Walker in WordPress 5.3
With te latest releases of WordPress, after updating to WP 5.3 it’s common to receive this error on some custom themes:
Warning: Declaration of _Nav_Menu::walk($elements, $max_depth) should be compatible with Walker::walk($elements, $max_depth, …$args) in…
This is caused by the introduction of the spread operator, as per official WordPress release note:
In WordPress 5.3, the PHP 5.6 spread operator has been introduced to WordPress in a number of places.
Using the spread operator allowed for simplifying the code and improves performance – both in speed as well as memory use -, especially as it has been introduced in a number of functions which are used a multitude of times during every single pageload, such as current_user_can()
and add_query_arg()
.
Most plugins and themes should see no impact of these patches, other than the improved performance.
However, if your plugin/theme is impacted, you will need to take action.
In the file function.php we can use a workaround to adapt the Walk function and change it from:
function walk ($items, $depth) { $this->elements = $this->get_number_of_root_elements($items); return parent::walk($items, $depth); }
To:
function walk ($items, $depth,...$args) { $this->elements = $this->get_number_of_root_elements($items); return parent::walk($items, $depth,...$args); }
Hope this helps someone with the same problem, feel free to comment below!
Roberto