2

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

3

The get_search_form() echos so it will always show up before returns. Use:

get_search_form( false )
  • My god, it really worked. I've been trying get_search_form( 'false' ) without success because I because I saw it in a post. Thank you. – SilverLink Mar 9 '17 at 9:03
3

The solution is to use get_search_form( false ).

get_search_form() will output the search form. Since you are trying to 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.