I want change The - to +
automatically, No Manual.
http://website.com/What-day-is-today/
to:
http://website.com/What+day+is+today/
Dbranes answers:
You can try this:
add_filter( 'wp_unique_post_slug', 'my_slug');
function my_slug($slug){
$from="-";
$to="_";
$slug = str_replace($from,$to,$slug);
return $slug;
}
where you can change <strong>$to</strong> to your needs, but I'm not sure if using <strong>+</strong> is good thing, since it might be considered as a space in the url.
<strong>edit:</strong>
echo urlencode("here is a text with some spaces ");
this will have the output:
here+is+a+text+with+some+spaces+
And if you try
echo urldecode("here+is+a+text+with+some+spaces+");
you will get
here is a text with some spaces
So I wouldn't use +, consider underscore instead if you want to change it ;-)
combize comments:
in functions.php?
Dbranes comments:
yes, in functions.php
combize comments:
I put the code in functions.php
And it did not do anything
Dbranes comments:
try insert a new post
combize comments:
Now it works :)
You can also search code
Thank you
Dbranes comments:
ok great, good luck with your project.
combize comments:
Can you give me Same code to search
Dbranes comments:
can you explain a little bit more what you mean?
combize comments:
Same code just to Search
http://website.com/search/What+day+is+today/
to:
http://website.com/search/What-day-is-today/
Dbranes comments:
ok, but why would you want that ;-)
maybe something like
add_filter('get_search_query', 'my_search');
function my_search($s){
$from="-";
$to="+";
$s=str_replace($from,$to,$s);
return $s;
}
will test it when I get back online later.
combize comments:
It does not work
Dbranes comments:
I'm back, looks like the filter we where hooking into is firing to late.
Try this instead
add_filter('pre_get_posts','my_search2');
function my_search2($query){
if($query->is_main_query() && $query->is_search()){
$from="-";
$to="+";
$query->query_vars['s']=str_replace($from,$to,$query->query_vars['s']);
}
return $query;
}
This should allow you to use - as + in your search query.
combize comments:
Not working
Still have +
Dbranes comments:
This code will enable you to have url like this
http://website.com/search/What-day-is-today/
and it will give you a search result - does this work like that for you?
You also want to replace the url generated from the search forms when you press submit?
(the project is getting longer)
combize comments:
Give me your Skype
I pay you
combize comments:
Still have + in url search I want - in url
Dbranes comments:
There are two parts we need to cover for the "-" enabled search
1) Handle "-" in the search url.
2) Change the search parameter so it contains "-" instead of "+", when we submit the search form.
1) This part should be covered by the code above.
2) For this part we need to use javascript/jQuery:
Here is one idea to change the spaces into minuses (-) when we are writing the search string:
add_action('wp_enqueue_scripts', 'my_enqueue' );
function my_enqueue() {
wp_enqueue_script( 'jquery');
}
//
// let's add this into the footer just for testing
//
add_filter("wp_footer","my_search_input_fix");
function my_search_input_fix(){
echo '<script>jQuery("input#s").keyup(function() {jQuery(this).val(jQuery(this).val().replace(/\s/g, "-"));});</script>';
}
where we target the keyup event of a text input field with <strong>id="s"</strong>.
If this works for you, then you should save the javascript into a file in your theme directory and enqueue it also.
combize comments:
Dbranes,
Thank you for the help :)
You are the King!!!
Dbranes comments:
Here is a wrap up (with some changes):
add_action('wp_enqueue_scripts', 'my_enqueue' ); // enqueue jQuery
add_filter("wp_footer","my_search_input_fix"); // jQuery stuff to replace space to minus on searchform submit (better if enqueued)
add_filter('pre_get_posts','my_search'); // allow minuses in the search url
//add_filter( 'wp_unique_post_slug', 'my_slug'); // change post slug
function my_enqueue() {
wp_enqueue_script( 'jquery');
}
function my_search_input_fix(){
echo '
<script>
jQuery("form#searchform").submit(function() {
jQuery("input#s").val(jQuery("input#s").val().replace(/\s/g, "-"));
});
</script>';
}
function my_search($query){
if($query->is_main_query() && $query->is_search()){
$query->query_vars['s']=str_replace("-"," ",$query->query_vars['s']);
}
return $query;
}
function my_slug($slug){
$from="-";
$to="_";
$slug = str_replace($from,$to,$slug);
return $slug;
}
combize comments:
What is your Skype?
Dbranes comments:
sent you a PM earlier today
combize comments:
Ok you the best :)
Francisco Javier Carazo Gil answers:
In wp-admin/options-permalink.php go to custom structure and set:
/post+%postname%/
Then save changes.
combize comments:
This is not good
Francisco Javier Carazo Gil comments:
Which is the problem?
combize comments:
It does this:
http://dubit.co/post+post-name/
combize comments:
http://website.com/What-day-is-today/
to:
http://website.com/What+day+is+today/
Francisco Javier Carazo Gil comments:
Ah ok, I understand. Look at dbranes, it is not a good idea using + characters into an URL.