0

I made a searchform.php file to custom my search widget as I read, but when I use the search widget, it doesn't make the searches well, it sarches in the page of the categorie i'm writing. Is there anything wrong with my code?

<form role="search" method="get" id="searchform" class="searchform" action="<?php  esc_url( home_url( '/' ) ); ?>">
    <div>
        <label class="screen-reader-text" for="s" ><?php _x( 'Search for:', 'label' ); ?> </label>
        <input type="text" value="<?php get_search_query(); ?>" placeholder="Buscar..." name="s" id="s" />
        <input type="image" src="<?php echo get_stylesheet_directory_uri(); ?>/images/buscar.png" style="width:40%;" id="searchsubmit" value="<?php esc_attr_x( 'Search', 'submit button' ); ?>" />
    </div>
</form>

1 Answer 1

1

action="<?php esc_url( home_url( '/' ) ); ?>">

You forgot to echo out the URL, making the action attribute this:

action=""

This means the form is submitted to the current page, and since the current page is a category archive, it appends the s parameter to search inside that archive.

Normally you would instead use the homepage URL where WP will then load the search.php template and do a full search

2
  • I don't know what happened, but now it send me to the home page insead of searching... <form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>"> Sep 6, 2017 at 2:02
  • Is that not what you wanted? Hence home_url( '\'), what happens if you visit /?s=test? On my site and any standard site it would bring up search results for the word test, if that's not the case on your site then something is terribly wrong/interfering with normal search functionality
    – Tom J Nowell
    Sep 6, 2017 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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