Support » Developing with WordPress » Change location of searchform.php

  • Resolved exuraz

    (@exuraz)



    Hi,

    I’m trying to change the location of the searchform.php file. Instead of the theme root, I want it to be in a folder.

    I managed to load it into the sidebar by adding:
    add_filter("get_search_form", 'custom_get_search_form');
    and then custom_get_search_form() does a locate_template() to properly return my custom searchform.php in a folder.

    However, now there are two search forms.
    How do I get rid of the default WordPress one?

Viewing 8 replies - 1 through 8 (of 8 total)
  • How is the first one being displayed? Is it a direct call to get_search_form() in a template file? Is it a widget in the sidebar? Is it also being called by a hook you can remove? Odds are you’ll have to either remove it from the theme file directly or remove the hook adding it if you can find it.

    Yeah it’s automtically added in the sidebar. Where would I look for such a hook?

    Moderator bcworkz

    (@bcworkz)

    A quick and dirty approach would be to simply hide the original search form with CSS. Add the required CSS to the Additional CSS panel of the customizer.

    The truly correct approach is as Howdy suggests in removing the enabling code.

    get_sidebar hook in functions.php possibly.

    First check Appearance -> Widgets and see if it’s being added there, if so you can just simply remove it from whatever widget section it’s in.

    @bcworkz Yeah though I’d rather do a non-dirty approach haha. The enabling code seems to be in the part that grabs the widgets. I already replaced the searchform function with my own in some template files, but the one in the sidebar remains.

    @howdy_mcgee How would I catch that hook and tell it to load a different searchform? I’m a bit confused with things like this in WordPress. It would make much more sense to me if the sidebar template file has like a ‘load widgets’ function that I would be able to modify to load a different search form. But it just seems to appear out of thin air.

    Moderator bcworkz

    (@bcworkz)

    get_sidebar is an action where what template is loaded cannot be managed. It could be used to directly generate output before the template is loaded, but not for what template is loaded. You need a filter for that. To load a different template, you can use the “template_include” filter. But if a different template still calls dynamic_sidebar(), the same widgets could still be loaded.

    You could blindly try to override things so the original form does not appear, but ideally you should locate the code that is causing the form output and directly override whatever technique is used to add the form. Unfortunately, there are several ways to add forms like this, it can indeed seem to appear like magic. Like magic though, it’s all an illusion and there is a concrete cause somewhere.

    Could you link to the site you’re trying to remove the search form? Maybe peering at the HTML will give viewers a better idea of where the code may lie.

    @bcworkz I don’t think it’s the sidebar template itself. I do use get_sidebar() but that template doesn’t include anything regarding a search form or any other widget. I assume the widgets are loaded somewhere else. And when the search widget is active, it calls get_search_form() and retrieves either the default one or a ‘searchform.php’ file in the project root. This all works, but I want the location of the searchform.php to be different.

    @howdy_mcgee
    The site is currently running locally, but this is what it looks like with my custom searchform.php through placing that file in the theme root. https://i.imgur.com/ek1UmPN.png. I can get one to load by add_filter("get_search_form", 'custom_get_search_form'); through custom_get_search_form(). But then it loads both. It should only load my custom one.

    For something like a custom header, this function looks like:

    $templates[] = "templates/{$type}.php";
    $templates[] = "{$type}.php";
    locate_template( $templates, true, false );

    So that it looks for a template file in templates/ first, and then in the theme root. It should do that for the searchform.php as well.

    EDIT:
    I found it.

    		
    function custom_get_partial( $type, $name = null ) {
        $templates[] = "templates/{$type}.php";
        $templates[] = "{$type}.php";
    	locate_template( $templates, true, false );
    }
    
    function custom_get_search_form( $echo ) {
    	custom_get_partial('searchform', null );
    	return false;
    }
    add_filter("get_search_form", 'custom_get_search_form');

    So basically sets echo to false on the default search form and instead calls a function that gets my custom one.

    • This reply was modified 2 months, 1 week ago by  exuraz.
    • This reply was modified 2 months, 1 week ago by  exuraz.
    • This reply was modified 2 months, 1 week ago by  exuraz.
Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.