Wordpress not filtering posts - php

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>

Related

Control Search Parameters in PHP Wordpress

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>

Magento search box on Wordpress site

I have a WP site on http://subdomain.example.com and a Magento site at http://example.com.
I was wondering what is the easiest way to add a Magento search box on the WP installation?
This is the WP search box code, but i dont know what parameter to modify since i have no idea how Magento works :(
<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>
Any help would be greatly appreciated!
You can make a php script that gets the data from the magento site every time someone searchs . that would probably be the simplest way.
Put the Magento catalogsearch url in the form action, and an input field with name attribute value "q" such as:
<form method="get" action="http://example.com/catalogsearch/result/">
<input type="text" value="" name="q" />

Wordpress search function not working

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>

How to make placeholder for search input in WordPress

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"/>

Have different placeholders for search inputs on the same page - wordpress

If you use the searchform.php in wordpress to show search input boxes. How can you show different placeholders in different search boxes on the same page? For example, I have one page that has a search box in the footer, in a widget in the sidebar and on the main navigation menu. I wish to show different placeholders on each one.
This is what i've got in my searchform
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
<input id="s" placeholder="Search..." type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';"
onblur="if (this.value=='') this.value = this.defaultValue" >
<input class="searchsubmit" type="submit" value="<?php _e( '', 'mytheme' ); ?>">
</form>
<!-- /Searchform -->
I am not really sure what you are trying to do, but the general idea is pretty simple. Just create the placeholder text conditionally.
if (is_home()) {
$placeholder = "Search Posts...";
} elseif(is_tag()) {
$placeholder = "Search Tags...";
} else {
$placeholder = "Search...";
}
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
<input id="s" placeholder="<?php echo $placeholder; ?>" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';"
Is that something like what you are looking for?
Based on some further explanation of the requirements
The search form functionality isn't really meant to work like you want. You can cheat around it with global variables.
Put
$GLOBALS['placeholder'] = "Default placeholder value";
in functions.php then edit your form to read...
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
<input id="s" placeholder="<?php echo $GLOBALS['placeholder']; ?>" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';"
And call the function like:
$GLOBALS['placeholder'] = "Placeholder value...";
get_search_form();
According to the docs, get_search_form(false) should return a string, which means that something like this should work:
Edit the search form to read:
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
<input id="s" placeholder="%s" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';"
And then use...
$search_form = get_search_form(false);
printf($search_form,'Placeholder...');
That avoids the globals, which is generally a good thing. I could not that to work out of the box. If you look at the source, that functionality seems to be dependent upon the presence or absence of searchform.php. If that file exists it is simply included, which makes the "return a string" part pretty dicey. And it has proven surprisingly difficult to hijack using the available filters.
To avoid the global juggling, I think you will need to replace `get_search_form', for example, with:
function my_get_searchform($placeholder = 'Search') {
$search_form_template = locate_template('searchform.php');
if ( '' != $search_form_template ) {
ob_start();
require($search_form_template);
$form = ob_get_clean();
printf($form,$placeholder);
}
}
The get_search_form() function prints out the code from searchform.php. Instead of using get_search_form() in your page, take out that function and copy the code from searchform.php and paste it where you want the search bar to show. Then change the placeholder text accordingly.
<html>
<head>
<!-- Header Stuff -->
</head>
<body>
<div class="main-navigation-menu">
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
<input id="s" placeholder="PLACEHOLDER TEXT HERE" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';"
onblur="if (this.value=='') this.value = this.defaultValue" >
<input class="searchsubmit" type="submit" value="<?php _e( '', 'mytheme' ); ?>">
</form>
<!-- /Searchform -->
</div>
<div class="sidebar">
<div class="widget">
<!-- Default widget stuff from functions.php will appear here. Searchform styling will come from searchform.php -->
</div>
</div>
<div class="footer">
<!-- Searchform -->
<form method="get" class="search" action="<?php echo home_url(); ?>" >
<input id="s" placeholder="PLACEHOLDER TEXT HERE" type="text" name="s" onfocus="if (this.value==this.defaultValue) this.value = '';"
onblur="if (this.value=='') this.value = this.defaultValue" >
<input class="searchsubmit" type="submit" value="<?php _e( '', 'mytheme' ); ?>">
</form>
<!-- /Searchform -->
</div>
</body>
</html>
Keep the searchform.php and style that file how you want your widget search form to appear.

Categories