I've got a website on Wordpress and someone in my team updated something and now there is an error on that website:
Warning: Illegal string offset 'output_key' in /.../wp-includes/nav-menu.php
I disabled the debug message and now the site is working fine. I guess this is not the best solution. Hours of googling and looking for something here brought me to the fact that I have to tell the script that 'output_key' is an array element.
$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
$args = wp_parse_args( $args, $defaults );
$args['include'] = $items;
if ( ARRAY_A == $args['output']) {
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
usort($items, '_sort_nav_menu_items');
$i = 1;
foreach( $items as $k => $item ) {
$items[$k]->$args['output_key'] = $i++; //here is the error
}
}
I tried to ask $args if it is an array in line 1.
$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
$args = wp_parse_args( $args, $defaults );
$args['include'] = $items;
if ( ARRAY_A == $args['output'] && is_array($args) ) {
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
usort($items, '_sort_nav_menu_items');
$i = 1;
foreach( $items as $k => $item ) {
$items[$k]->$args['output_key'] = $i++; //here is the error
}
}
But line 6 still shows an error on the website. I don't know what to do with that line.
It seems that you are using an outdated version of wordpress which is no longer compatible with PHP 7.
See migrating from php 5.6 to php 7 section about variable handling.

Which means that the expression $items[$k]->$args['output_key'] is interpreted as:
$items[$k]->{$args['output_key']} in PHP 5($items[$k]->$args)['output_key'] in PHP 7It also seems that this issue is fixed in recent wordpress code.
To fix the issue manually, just replace the code:
$items[$k]->$args['output_key'] = $i++; // here is the error
with:
$items[$k]->{$args['output_key']} = $i++; // problem solved :)
You should also consider upgrading entire wordpress installation to make sure that the rest of the code is compatible with PHP7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With