简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM