How to turn "+" into "-" in wordpress search result URL? - wordpress

According to the online ones, adding the following code to functions.php will enable the URL modification.
function tongleer_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'tongleer_search_url_rewrite' );
When I searched for "2015 bmw"
The result obtained is: my-domain.com/search/2015+bmw
It will turn the space into a "+"
How to change the "+" to "-" in the url? And end with add "/". For example. my-domain.com/search/2015-bmw/

Related

How to change search URL in wordpress?

I want to change the search url to something like this
http://simply.mmag.in/blog/?s=coaching
Where as my current search result shows which is recommended by most user and article
http://simply.mmag.in/blog/coaching
Code which i am using for the above search result
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
and the base url code
function re_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->search_base = 'blog';
$wp_rewrite->flush_rules();
}
add_action('init', 're_rewrite_rules');
How can i get search result like this
http://simply.mmag.in/blog/?s=coaching
What happends if you just remove or comment out your wpb_change_search_url() function?
Have you tried to just remove this custom code? Wordpress by default uses ?s= in urls, and code used by you just rewrite it.

Custom Product Search URL in Wordpress

Hi I have tried the following code and does seem to work and changes the regular search url for a keyword "holi"
https://www.englishbix.com/search/?s=holi&post_type=product to https://www.englishbix.com/search/holi/
but it uses the default search template to display the product search results instead of the woocommerce search result template.
//change the url to static slug instead of dynamic one
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
Results I want
Results I get with above code

WordPress search not working for particular word / It is working when I am manually add in URL

Wordpress Search
1). www.example.com/?s=perticularword - Not Working
example particular word:"booking"
2). www.example.com/blog/?s=booking - Entered manually in URL it is working
3). If I am using the code - It returns too many redirections - Not Working
function fb_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/?s=" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
I want www.example.com/blog/?s=booking this search results how to get results ??
Your if condition is also true for your redirected url. Use
if ( ( is_search() && ! empty( $_GET['s'] ) && stripos( $_SERVER['REQUEST_URI'], 'blog' ) === false ) {
to make the condition false on your redirected url.

How do i change the url of wordpress search results?

by default wordpress search results go to sitename.com/?s=terms but i would like it be changes to sitename.com/search/?s=terms
I have tried using the
fb_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
This get close as now when you do search it goes to search/searchterm but does not have the ?s= in the url
Alternatively, you can change the rule in .htaccess
Add the following on the line after "RewriteBase /":
RewriteRule ^?s=(.*)$ search/?s=$1
See reference here: http://wordpress.org/support/topic/change-default-search-string-s-to-permalink-structure

Wordpress site in subdirectory, logout function failing

We have Wordpress site in the root of our domain. A translation plugin we use appends the domain with (in our case for Czech) /cs - this means we can run more than one translation but use the same database and wp-content as the main English website.
However, the added /cs causes the logout function to fail as it tries to use the current directory as the basis for where the actual site content is being pulled from.
The actual code being used is <?php echo wp_logout_url( $redirect ); ?>. We have tried a simple HTML href but the logout link is dynamic and requires a unique nonce value to validate the command.
Do you have any ideas for how we can have a logout button that uses the actual site address (mywebsite.com) rather than with the added 'directory' (mywebsite.com/cs). I have thus far been unable to edit the wp_logout_url to add a / before it. Any ideas?
Example links:
Correct:
http://www.mywebsite.com/backend?action=logout&redirect_to=index.php&_wpnonce=d8eaf8594a
Incorrect, resulting in 404 ERROR:
http://www.mywebsite.com/cs/backend?action=logout&redirect_to=index.php&_wpnonce=d8eaf8594a
Actual code being used (relevant logout code is the #bawlogout# part):
add_filter( 'wp_setup_nav_menu_item', 'bawllm_setup_nav_menu_item' );
function bawllm_setup_nav_menu_item( $item )
{
global $pagenow;
if( $pagenow!='nav-menus.php' && !defined('DOING_AJAX') && isset( $item->url ) && strstr( $item->url, '#baw' ) != '' ){
$item_url = substr( $item->url, 0, strpos( $item->url, '#', 1 ) ) . '#';
$item_redirect = str_replace( $item_url, '', $item->url );
switch( $item_url ) {
case '#bawloginout#' :
$item_redirect = explode( '|', $item_redirect );
if( count( $item_redirect ) != 2 )
$item_redirect[1] = $item_redirect[0];
for( $i = 0; $i <= 1; $i++ ):
if( $item_redirect[$i] == '%actualpage%')
$item_redirect[$i] = $_SERVER['REQUEST_URI'];
endfor;
$item->url = is_user_logged_in() ? wp_logout_url( $item_redirect[1] ) : wp_login_url( $item_redirect[0] );
$item->title = bawllm_loginout_title( $item->title ) ; break;
case '#bawlogin#' : $item->url = wp_login_url( $item_redirect ); break;
case '#bawlogout#' : $item->url = wp_logout_url( $item_redirect ); break;
case '#bawregister#' : if( is_user_logged_in() ) $item->title = '#bawregister#'; else $item->url = site_url( '/wp-login.php?action=register', 'login' ); break;
}
$item->url = esc_url( $item->url );
}
return $item;
}
I would put the URL into a variable so that you can perform a regular expression on it, for example:
<?php
$url = wp_logout_url( $redirect );
$fixed_url = preg_replace("/stuff_to_find/", "stuff_to_replace", $url);
?>
In the end I took the easy way out and use a Redirect 301 in .htaccess to redirect that logout link with /cs/ to /

Resources