Search parameters are null in PHP Wordpress
When user press search button, it make query string parameters even if search fields are empty
www.example.com/?ssc=&sst=&s=
How i can check it and remove (Don't Allow) null parameters from url
If you know where your form is situated, add required to input text
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" required />
or else create searchform.php in your theme directory and add following code
<form action="/" method="get">
<label for="search">Search in <?php echo home_url( '/' ); ?></label>
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" required />
<input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" />
</form>
Related
In my WordPress v5.5.1, I have custom post 'song'.
I have the below search form in my header for all pages:
<form>
<input type="hidden" value="song" name="post_type" id="post_type" />
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>">
</form>
And the search results are shown in search.php.
The search form works in all pages except the custom post_type page www.example.com/song/song-1, search query goes to 404 page not found.
Found that upon adding get and action to the form tag as below, it solves the issue:
<form class="search-form" method="get" action="<?php echo bloginfo('url') ?>">
<input type="hidden" value="song" name="post_type" id="post_type" />
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>">
</form>
I am fleshing out my first custom Wordpress theme and trying to hook up the search. Now I am following the codex but does not appear to be working correctly. Perhaps I am doing something wrong?
I have added the theme support as this is using the HTML field method.
so here is my search field:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<input type="search" class="search-field" placeholder="What are you looking for?" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
</label>
<br />
<button class="button-search">Find items</button>
</form>
And I have created a search.php with the usual template page bits along the top, created the search page as a page in wordpress, yet when I search I can see the post request in the URL but the page is blank. I am cleary doing something wrong here?
This should work for you
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label>
<input type="text" class="search-field" placeholder="What are you looking for?" value="<?php echo get_search_query() ?>" name="s" id="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
</label>
<br />
<input type="submit" class="button-search" id="searchsubmit" value="Find items" />
</form>
I'm making a Wordpress template on my own. On the top of the header, there is a fixed search input, so the user can search for any posts with a determinate tag whenever he wants to.
The problem is that when I type a tag keyword on the input, it always shows all the posts, not filtering any of it at all.
But when I search for a keyword that has no tag registered, it returns a 404.php error page.
One curious thing is that when there is a tag keyword, the link appears like this:
/wordpress/tag/name-of-the-tag/
And when there is no tag with that keyword, it appears as:
/wordpress/?tag=name-of-the-tag/
My form code is right below:
<form method="get" action="<?php echo esc_url( home_url( '/' ) );?>">
<input id="s" type="text" name="tag" onfocus="if (this.value=='Buscar...') this.value = '';" onblur="if (this.value=='') this.value = 'Buscar...'" name="search" class="form-search" value="Buscar..."/>
</form>
You've add the name attribute twice. With a value tag and search. Remove all of them and add a name="s"
This is from CODEX
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url(home_url('/')); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _x('Search for:', 'label'); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="<?php echo esc_attr_x('Search', 'submit button'); ?>" />
</div>
</form>
I've override default wordpress search form by creating my own search form (found this code in google). here it is:
<form method="get" class="searchform" action="<?php bloginfo('url'); ?>">
<div>
<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" />
</div>
</form>
I tried to add a place holder for this & change my code for input line to this:
<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" placeholder="Search Here" />
but it is not working for me. please help someone.
you can simply use the placeholder attribute like so:
<input type="text" placeholder="Search the site"/>
If you put a value, the value will overwrite your placeholder. Try to delete the value from your input and it should work
You can just use placeholder on your input tag like
<input type="text" placeholder="Search the site"/>
How do I make this code (custom):
<div id="sb-search" class="sb-search">
<form>
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="search" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
</div>
retrieve search results like this one ( wordpress ) does:
<form method="get" class="searchform" action="<?php echo home_url('/'); ?>">
<div>
<input type="text" class="search" name="s" onblur="if(this.value=='')this.value='<?php _e('To search type and hit enter','typegrid'); ?>';" onfocus="if(this.value=='<?php _e('To search type and hit enter','typegrid'); ?>')this.value='';" value="<?php _e('To search type and hit enter','typegrid'); ?>" />
</div>
Basically I got this custom code for an awesome search box with on-click slide etc. It has a field where people can type, but it gets no results as used on my wordpress site. I wanna make it able to get results from wordpress content w/o changing divs thus not altering its design. Please help, I'm stuck!!
This would do it:
<div id="sb-search" class="sb-search">
<form action="<?php echo home_url('/'); ?>">
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="s" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
</div>
All you have to do is add the form action, and make the name of the search input to be "s".
<form method="get" class="searchform" action="<?php echo home_url('/'); ?>" >
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="s" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
homeurl ?s=anything will call wordpress search function, so just make form's action GET and ACTION = your home url