I have really tried my best but nothing is working out,
I want to replace my wp search permalink [spaces]
and +
to -
Example:
love you
to
and also
+love+you
to
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.
/**
* Replace the search permallink.
*
* @param array $search_rewrite
* @return string
*/
function ssp_change_search_permalink( $search_rewrite ) {
global $ssp_settings;
if( ! is_array( $search_rewrite ) ) {
return $search_rewrite;
}
$new_rewrite = array();
foreach ( $search_rewrite as $pattern => $s ) {
$regex = str_replace( 'search/', "$ssp_settings[ssp_permalink]/", $pattern );
$new_rewrite[$regex] = $s;
}
$search_rewrite = $new_rewrite;
unset( $new_rewrite );
return $search_rewrite;
}
/**
* 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();
}
}
/**
* Keep search structure.
*
* @param $object $query
*/
function ssp_keep_structure( $query ) {
global $wp_query;
if ( $query->get('s') && empty( $_GET['s'] ) && $wp_query->is_main_query() ) {
$s_vars_tmp = urldecode( $query->get('s') );
$ssp_separate_symbol = array( '/', '_', '+', '-' );
$s_vars_tmp = str_replace( $ssp_separate_symbol, ' ', $s_vars_tmp );
$query->set( 's', $s_vars_tmp );
}
}
/**
* Remove unwanted characters.
*
* @param string $query_strings
* @return string
*/
function ssp_filter_spec_chars($query_strings) {
global $ssp_settings;
if ( $ssp_settings['ssp_filter_character_option'] == 2 ) {
$ssp_unwanted_characters = array( '"', "'", '[', ']', '~', '=', ',', '*', '&', '^', '$', '#', '@', '!', '<', '>', ';', '.', '|', '/', '+', '-', '_' );
}
if ( is_array( $ssp_unwanted_characters ) ) {
$query_strings = trim( str_replace( $ssp_unwanted_characters, ' ', $query_strings ) );
}
return $query_strings;
}
/**
* Remove bad words.
*
* @param string $query_strings
* @return string
*/
function ssp_filter_bad_words( $query_strings ) {
if ( '' !== $ssp_settings['ssp_filter_words'] ) {
$ssp_bad_words = explode( ',', $ssp_settings['ssp_filter_words'] );
$query_strings = str_replace( $ssp_bad_words, ' ', $query_strings );
}
$query_strings = preg_replace( '/\s{2,}/', ' ', $query_strings );
return $query_strings;
}
//============================================
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Assign default setting values while activating SSP.
*/
function ssp_admin_activation() {
add_option( 'ssp_permalink', '' );
add_option( 'ssp_letter_type_option', '1' );
add_option( 'ssp_filter_words', '' );
add_option( 'ssp_filter_character_option', '1' );
add_option( 'ssp_separate_symbol_option', '1' );
}
// Activating plugin.
register_activation_hook( plugin_dir_path( __FILE__ ), 'ssp_admin_activation' );
/**
* Register the plugin setting page.
*/
function ssp_admin_option_hook() {
if ( function_exists( 'add_options_page' ) ) {
add_options_page(
__( 'SEO Search Permalink', 'ssp-plugin' ),
__( 'SEO Search Permalink', 'ssp-plugin' ),
'manage_options',
'seo-search-permalink.php',
'ssp_administrator_page'
);
}
}
// Load SSP settings.
$ssp_settings = array(
'ssp_permalink' => get_option( 'ssp_permalink' ),
'ssp_filter_words' => get_option( 'ssp_filter_words' ),
'ssp_filter_character_option' => get_option( 'ssp_filter_character_option' ),
'ssp_separate_symbol_option' => get_option( 'ssp_separate_symbol_option' ),
'ssp_letter_type_option' => get_option( 'ssp_letter_type_option' )
);
function ssp_init() {
load_plugin_textdomain( 'seo-search-permalink', false, basename( dirname( __FILE__ ) ) . '/languages' );
// Only load SSP setting page in admin panel.
if ( is_admin() ) {
require_once plugin_dir_path( __FILE__ ) . 'inc/setting.php';
// Detect form submitting in setting page.
ssp_update_form_options();
}
// Load SSP functions.
require_once plugin_dir_path( __FILE__ ) . 'inc/functions.php';
// The magical things are happened here.
add_filter( 'search_rewrite_rules', 'ssp_change_search_permalink' );
add_action( 'admin_menu', 'ssp_admin_option_hook' );
add_action( 'template_redirect', 'ssp_search_redirect' );
add_action( 'pre_get_posts', 'ssp_keep_structure' );
}
add_action( 'init', 'ssp_init' );
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?