Custom search form

sage9

#1

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

Have you tried exactly that? Does it not work?


#3

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


#4

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’)


#5

@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;  
});

#6

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


#7

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/


#8

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


#9

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>

#10

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;
});

#11

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' );
});

#12

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?