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