Home > OS >  Issue with passing variables by reference in PHP
Issue with passing variables by reference in PHP

Time:10-15

I have this notice message in the frontend of my Wordpress:

"Notice: Only variables should be passed by reference in..."

The notice is for this line of PHP code:

$parent_id = array_pop( wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' ) );

Here is the full PHP code:

add_filter( 'wp_nav_menu_objects', 'get_submenu', 10, 2 );
function get_submenu( $items, $args ) {
    if ( empty($args->submenu) ) {
        return $items;
    }

    $parent_id = array_pop( wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' ) );
    $children  = submenu_get_children_ids( $parent_id, $items );

    foreach ( $items as $key => $item ) {

        if ( ! in_array( $item->ID, $children ) ) {
            unset($items[$key]);
        }
    }

    return $items;
}
function submenu_get_children_ids( $id, $items ) {
    $ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );

    foreach ( $ids as $id ) {
        $ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
    }

    return $ids;
}

I don't success to remove the notice by resolving the issue. Thanks in advance for the help.

CodePudding user response:

Try this:

$filter_items = wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' );
$parent_id = array_pop( $filter_items );

The reason is that the wp_filter_object_list array is passed by reference because it is modified by the function array_pop. It means that you need to pass array_pop a real variable and not a function.

  • Related