How can I replace a character in a wordpress search query? - php

For example, how would I go about replacing www.example.com/search/mark+twain with www.example.com/search/mark_twain ?
The form to search looks like this:
<form role="search" method="get" id="searchform" action="<?php echo home_url('/'); ?>">
<label class="visuallyhidden" for="s"><?php _e('Search for:', 'roots'); ?></label>
<input type="text" value="" name="s" id="s" placeholder="<?php _e('Search Courses/Material'); ?> ">
<input type="submit" id="searchsubmit" value="<?php echo attribute_escape(__('Search')); ?>" class="button">
</form>

I'm not quite sure how you're using this or where exactly you're using that query, but you could always use string replace:
str_replace('+','_',$query)
That's the best answer I can give with this vague a question.
PHP Manual
For jquery (which wordpress makes use of) you could do something like:
$('#sear form').submit(function(){
$(this).val($(this).val().replace(' ','_'));
});
I think that's correct. I'm not where I can try it, but it should work.

Related

How can I add custom search box on page?

I'm using custom WordPress template and I don't know how to add search box in a specific place. Of course my template contains search box, but it is in a weird place, so I would like to add custom one. I cannot do that by adding widget. I have to edit index.php to add search box.
When I add this:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="btn" />
</div>
</form>
Basically, it is working. It is searching properly, but the search box is bad looking.
That one works as well:
<form method="get" id="search_form" action="<?php bloginfo('home'); ?>"/>
<input type="text" class="text" name="s" value="SEARCH" >
<input type="submit" class="submit" value="" />
</form>
But again, it looks awfull. I have to have custom css too.
And another one:
<?php get_search_form(); ?>
Works good, but the same problem. It is just like in 2000.
I tried adding custom css, but the problem is that id does not not searching anything on my website. Instead going to http://example.com/?s=WHAT-I-TYPED it goes to http://example.com/
What can I do with that?
To solve the UGLYNESS problem: Just add the plugin 'simple custom css': https://nl.wordpress.org/plugins/simple-custom-css/. Then write some CSS to make it pretty.
To solve the SEARCH problem replace name="search" with name="s" on your input field (source: https://developer.wordpress.org/reference/functions/get_search_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 not filtering posts

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>

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

Merge wordpress searchform code with custom html code

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

Categories