Custom search form

Using Sage 9, is there a way to create a replacement for the search form with Blade? Right now I just have a new file, searchform.php, in the base directory of the theme. I’d like to move it from there to /templates/searchform.blade.php, but I’m not sure what other changes I need to make in order to do that.

2 Likes

Have you tried exactly that? Does it not work?

I should have mentioned, I did try exactly that. It did not work.

I did it like this

https://github.com/kaisermann/selene/blob/master/src/filters.php#L170

Edit: I’m not sure about using config('view.paths')[0] or config('dir.stylesheet') . '/views/' ... (I use ‘views’ instead of ‘template’)

4 Likes

@kaisermann Thanks, that did the trick. I ended up doing this:

add_filter('get_search_form', function(){  
  $form = '';  
  echo template(realpath(config('dir.template') . '/templates/partials/searchform.blade.php'), []);  
  return $form;  
});
3 Likes

on version 9 beta 3

add_filter('get_search_form', function(){  
  $form = '';  
    echo template(realpath(get_template_directory() . '/views/partials/searchform.blade.php'), []);  
  return $form;  
});

an create the searchform.blade.php on views/partials/searchform.blade.php

2 Likes

In Sage 9 beta 4 I do like this

add_filter('get_search_form', function () {
  $form = '';
  echo template('partials.site-search-form');
  return $form;
});

My search form file is called site-seach-form.blade.php located in views/partials/

7 Likes

site-search-form.blade.php * :grin:

Here is how I change the form to apply blade style

<form role="search" method="get" class="search-form" action="{{ esc_url( home_url( '/' ) ) }}">
  <label>
    <span class="screen-reader-text">{{ _x( 'Search for:', 'label' ) }}</span>
    <input type="search" class="search-field" placeholder="{!! esc_attr_x( 'Search &hellip;', 'placeholder' ) !!}" value="{{ get_search_query() }}" name="s" />
  </label>
  <input type="submit" class="search-submit" value="{{ esc_attr_x( 'Search', 'submit button' ) }}" />
</form>
2 Likes

Alright, so to be clear… here’s how I added a custom search form using all of the above:

First, create a searchform.blade.php file in your partials folder and paste in the following:

<form role="search" method="get" class="search-form" action="{{ esc_url( home_url( '/' ) ) }}">
  <label>
    <span class="screen-reader-text">{{ _x( 'Search for:', 'label' ) }}</span>
    <input type="search" class="search-field" placeholder="{!! esc_attr_x( 'Search &hellip;', 'placeholder' ) !!}" value="{{ get_search_query() }}" name="s" />
  </label>
  <input type="submit" class="search-submit" value="{{ esc_attr_x( 'Search', 'submit button' ) }}" />
</form>

Then in your app/filters.php file, at the very bottom, paste in the following:

add_filter('get_search_form', function () {
  $form = '';
  echo template('partials.searchform');
  return $form;
});
8 Likes

Thanks for all the research. I found a cleaner way to implement the filter:

add_filter('get_search_form', function () {
  return \App\template( 'partials.searchform' );
});
16 Likes

Solutions by @matt-antone and @marcelo2605 our pretty neat. Sage developers, what do you think about adding this custom search form to the out-of-the-box version of Sage so that we don’t have to implement this manually every time we start a new sage theme?

8 Likes

Same thing in sage 10, except you use \Roots\view() instead of \App\Template. Also, there’s a built-in views/forms/search.blade.php for this already in sage 10!!

1 Like

With this filter, a searchform appears in the toolbal. Why?

05

Here is the way I did it using blade sintax:

  @include('partials.searchform')
2 Likes