• Resolved charmies

    (@charmies)


    Hello,
    I was really happy when I managed to add a search form to my main menu using the instructions on your site here http://www.themesandco.com/snippet/adding-an-html5-search-form-in-your-wordpress-menu/

    However, subsequently I had issues logging in to the admin area of WP. When I removed this code – and one previous to it – from the functions.php file in my child theme, all went back to normal.

    I have probably missed something obvious here, but any ideas what?

    // Exclude images from search results – Customizr
    add_action(‘init’, function(){
    remove_filter( ‘pre_get_posts’, array(TC_post_list::$instance, ‘tc_include_attachments_in_search’) );
    });
    // As of 3.1.10, Customizr doesn’t output an html5 form.
    add_theme_support( ‘html5’, array( ‘search-form’ ) );

    add_filter(‘wp_nav_menu_items’, ‘add_search_form_to_menu’, 10, 2);
    function add_search_form_to_menu($items, $args) {

    // If this isn’t the main navbar menu, do nothing
    if( !($args->theme_location == ‘main’) )
    return $items;

    // On main menu: put styling around search and append it to the menu items
    return $items . ‘<li class=”my-nav-menu-search”>’ . get_search_form(false) . ”;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • What kind of issue?
    Anyway in the code you posted something is missing:

    // On main menu: put styling around search and append it to the menu items
        return $items . '<li class="my-nav-menu-search">' . get_search_form(false) . '</li>';

    That ending “” .

    Thread Starter charmies

    (@charmies)

    It was the white screen of death!
    So should I try adding this to the end of the code?
    ”;

    Thanks

    Nope, I said you posted this:

    // On main menu: put styling around search and append it to the menu items
    return $items . '<li class="my-nav-menu-search">' . get_search_form(false) . '';
    }

    But ElectricFeet’s code was so:

    // On main menu: put styling around search and append it to the menu items
        return $items . '<li class="my-nav-menu-search">' . get_search_form(false) . '</li>';

    About white screen of death, probably you made some mistake in your functions.php, something that happens to all of us, something like a missing semicolon or closing bracket.

    What I suggest is to enable the debug mode so you can easily see what’s the problem. You will see an error printed instead of a white screen of death : https://codex.wordpress.org/WP_DEBUG

    Thread Starter charmies

    (@charmies)

    Strangely the closing li in this code didn’t display in my message.
    Debug didn’t pick up the problem so I removed all the code in functions.php and starting from scratch.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Issue when adding search form code to functions.php file’ is closed to new replies.