1

I wanted to add a search form inside a div using the code bellow:

printf( '<div class="entry"><p>%s</p>%s</div>', apply_filters( 'genesis_noposts_text', __( 'Try again below.', 'genesis' ) ), get_search_form() );

But every time I add the get_search_form() it's generated before the div, like the following:

<form></form>
<div class="entry"></div>

The workaround I found was to split the code, but I wanted to know if there is a better solution that works properly.

remove_action( 'genesis_loop_else', 'genesis_do_noposts' );
add_action( 'genesis_loop_else', 'mytheme_do_noposts' );
function mytheme_do_noposts() {

    printf( '<div class="entry"><p>%s</p>', apply_filters( 'genesis_noposts_text', __( 'Try again below.', 'genesis' ) ) );

    printf( '%s</div>', get_search_form() );

}

get_search_form documentation

SilverLink
  • 124
  • 2
  • 15

2 Answers2

1

The solution is to use get_search_form( false )

get_search_form() will output the search form. Since you are trying to use use the result in a string context, you need the function to return the html in form of a string, which you are passing through printf.

The only parameter for get_search_form controls that behavior. By default it prints, but if you pass false, it will return the string that you need.

yivi
  • 42,438
  • 18
  • 116
  • 138
SilverLink
  • 124
  • 2
  • 15
0

To get search form as a string use get_search_form( false ).

Use can see document of this function here.

MahdiY
  • 1,269
  • 21
  • 32