phpwordpresswp-nav-walker

Custom Nav Walker warnings


I have custom nav walker class that just adds some classes and replaces the url of some menu items. Menu is displayed correctly. The problem is there are a lot of warnings:

PHP Warning:  Attempt to read property "before" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 273
PHP Warning:  Attempt to read property "link_before" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 275
PHP Warning:  Attempt to read property "link_after" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 275
PHP Warning:  Attempt to read property "after" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 277
PHP Warning:  Attempt to read property "before" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 273
PHP Warning:  Attempt to read property "link_before" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 275
PHP Warning:  Attempt to read property "link_after" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 275
PHP Warning:  Attempt to read property "after" on null in /public_html/example.com/wp-includes/class-walker-nav-menu.php on line 277

class-walker-nav-menu.php

class Navigation extends Walker_Nav_Menu {
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat( "\t", $depth );
        $output .= "\n$indent<ul class=\"menu submenu\">\n";
    }

    function start_el(&$output, $data_object, $depth = 0, $args = null, $current_object_id  = 0)
    {
        $f_menu = get_field('menu_item', 'option');
        $s_menu = get_field('menu_item_second', 'option');

        if ($data_object->title == $f_menu) {
            $url = get_field('outbreak_plan', 'option');
            $data_object->url = $url['url'];
        } else if ($data_object->title == $s_menu) {
            $url = get_field('covid_updates', 'option');
            $data_object->url = $url['url'];
        }
        parent::start_el($output, $data_object, $depth = 0, $args = null, $current_object_id  = 0);
    }
}

I have other sites on this server that use the same class-walker-nav-menu.php with no warnings. How can I fix the code or make the server not show warnings for this file?


Solution

  • If anyone has the same problem, here is how the code should look like:

    class Navigation extends Walker_Nav_Menu {
        function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat( "\t", $depth );
            $output .= "\n$indent<ul class=\"menu submenu\">\n";
        }
    
        function start_el(&$output, $data_object, $depth = 0, $args = null, $current_object_id  = 0)
        {
            $f_menu = get_field('menu_item', 'option');
            $s_menu = get_field('menu_item_second', 'option');
    
            if ($data_object->title == $f_menu) {
                $url = get_field('outbreak_plan', 'option');
                $data_object->url = $url['url'];
            } else if ($data_object->title == $s_menu) {
                $url = get_field('covid_updates', 'option');
                $data_object->url = $url['url'];
            }
            parent::start_el($output, $data_object, $depth, $args, $current_object_id);
        }
    }
    

    So I just had to edit this line parent::start_el($output, $data_object, $depth, $args, $current_object_id);