Filter get_search_form() to return Sage 10 View

Hi there,

I’ve switched to working with Sage 10 for the most recent project and although I am getting more and more familiar with the new setup and things mostly work fine I’ve come across one issue I am not able to wrap my head around and would appreciate your help with:

With Sage 9 I could filter the get_search_form calls to return a custom blade template via

/**
 * Point WP to Sage's custom search form template
 */
add_filter('get_search_form', function() {
  return \App\template('partials.searchform');
});

whilst I am not able to do so within Sage 10 via

/**
 * Point WP to Sage's custom search form template
 */
add_filter('get_search_form', function() {
  return \Roots\view('dummy')->render();
});

The result here is that the filter ignores the custom template and just renders the default WP search form.

Calling the template directly via

{!! \Roots\view('dummy')->render() !!}

works though so that shouldn’t be the culprit.

Any thoughts on how to achieve this?

Thanks so much for sharing!

Another note regarding get_search_form():

Directly modifying and passing the form markup like proposed in the Codex docs (but make sure to add $args as a second param, the filter changed since 5.5.0) works as a temporary workaround but I’d like for the form markup to live in a partial preferably…

<?php
/**
 * Generate custom search form
 * @link https://developer.wordpress.org/reference/hooks/get_search_form/
 *
 * @param string $form Form HTML
 * @param array $args Form args
 * @return string Modified form HTML
 */
function custom_search_form($form, $args) {
  $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
  <div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
  <input type="text" value="' . get_search_query() . '" name="s" id="s" />
  <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
  </div>
  </form>';

  return $form;
}
add_filter('get_search_form', __NAMESPACE__.'\\custom_search_form', 40, 2);

I am seeing the same output as before and NOT the modified code I passed and returned…?

Using var_dump($form) before returning shows the proper version (indicated by the huhu-class) though. So

Which brings me to:

/**
 * Generate custom search form
 * @link https://developer.wordpress.org/reference/hooks/get_search_form/
 *
 * @param string $form Form HTML
 * @param array $args Form args
 * @return string Modified form HTML
 */
function custom_search_form($form, $args) {
  return \Roots\view('partials.searchform');
}
add_filter('get_search_form', __NAMESPACE__.'\\custom_search_form', 40, 2);

:tada:

Hey @evance - I’m a little confused, Acorn’s built in support for Sage already does this

Sage’s 404 template calls get_search_form which loads resources/views/forms/search.blade.php

1 Like

Well, that explains everything… :roll_eyes:

Thanks for clarifying…

1 Like