Replacing + sign to – in wp Search Permalink

I have really tried my best but nothing is working out,

I want to replace my wp search permalink [spaces] and + to –

Example:

https://wordpress.stackexchange.com/search/i love you

to

https://wordpress.stackexchange.com/search/i-love-you

and also

https://wordpress.stackexchange.com/search/i+love+you

to

https://wordpress.stackexchange.com/search/i-love-you

I found a plugin that can do that but I dont need plugin, I just need this on my theme function

Here is the link of code from the plugin that I think did it.

/**
* Redirect the search query to the new search permallink URL.
*/
function ssp_search_redirect() {
global $wp_rewrite, $ssp_settings;

if ( ! isset( $wp_rewrite )
|| ! is_object( $wp_rewrite )
|| ! $wp_rewrite->using_permalinks()
|| ! isset( $_SERVER[‘REQUEST_URI’] )
|| ” === $ssp_settings[‘ssp_permalink’]
) {
return;
}

$search_base = $wp_rewrite->search_base;

if ( is_search() && ! is_admin() && false === strpos( $_SERVER[‘REQUEST_URI’], “/$ssp_settings[ssp_permalink]/” ) ) {
$search_query_vars = ssp_filter_spec_chars( get_query_var(‘s’) );
$search_query_vars = ssp_filter_bad_words( $search_query_vars );

switch ( $ssp_settings[‘ssp_separate_symbol_option’] ) {
case 1: $search_query_vars = str_replace( ‘ ‘, ‘+’, $search_query_vars ); break;
case 2: $search_query_vars = str_replace( ‘ ‘, ‘-‘, $search_query_vars ); break;
case 3: $search_query_vars = str_replace( ‘ ‘, ‘/’, $search_query_vars ); break;
case 4: $search_query_vars = str_replace( ‘ ‘, ‘_’, $search_query_vars ); break;
}
if ( 2 === $ssp_settings[‘ssp_letter_type_option’] ) {
$search_query_vars = strtolower( $search_query_vars );
}
wp_redirect( home_url( “/$ssp_settings[ssp_permalink]/” . $search_query_vars ) );
exit();
}
}

But I dont need want to use plugin, Please gurus in the house help me out,

Is there anyway I can modify this line of code to work on theme function from the above task I mention?

Read more here:: Replacing + sign to – in wp Search Permalink

Leave a Reply

Your email address will not be published. Required fields are marked *