开发者

Search filter exact match

The search plugin I'm using in Wordpress, (Relavenssi), allows the user to add quotes to search terms in order to search for phrases, for example "search phrase".

How would I code it so that when the user selects a radio/check box called 'Exact Match', the quotes are automatically added to the search query rather than being added into the search box by the user?

Any help greatly appreciated, S.

开发者_JAVA技巧

Search form code below:

                  <form action="<?php bloginfo('home'); ?>/" method="post">
                  <div class="search-icon">     
                      <label for="search" accesskey="4" class="hidden">Search the site</label>                 
                      <input type="text" name="s" id="search" value="Enter search term" onblur="this.value = this.value || this.defaultValue;" onfocus="this.value = '';" />                
                      <input type="submit" name="submit" value="GO" class="s-btn" />
                      <p><a href="#" id="search-anchor">Search Options</a></p>
                      <div class="option-slide">
                      <input type="radio" name="sentence" value="" checked="checked" /><label>All Words</label><br />                          
                      <input type="radio" name="sentence" value="???" /><label>Exact Match</label>  
                      </div>   
                  </div>
              </form>      

Edit: Based on the answer from @Tristar Web Design below, I have now added this php code below my search form, although it doesn't work properly. It echoes out ok but isn't passing the query back. How do I pass/send the updated search query back to Wordpress?

                if(isset($_POST['s']) && $_POST['sentence'] == 'exact') {
                    $_POST['s'] = '"'.get_search_query().'"';
                    echo $_POST['s'];
                } else {
                    echo "2";
                }   


A couple of methods for this I guess.

  1. Use PHP and make it prepend and append quotes to the search value if exact match is selected.
  2. Use Javascript and make it prepend and append quotes inside the field onclick, so the value that is sent has quotes applied to it. I'd probably try this javascript method!

Edit:

Maybe consider this jQuery method (This method assumes that jQuery has been loaded, and I also added my own noConflict() variable) -

<script type="text/javascript">
    var $j = jQuery.noConflict();

    function addquo() {
    if($j('#s').val()) {
    var text = '"'+ $j('#s').val() +'"';
            $j('#s').val(text);
    }
    }

    function removequo() {
    if($j('#s').val()) {

    var txt = $j('#s').val();
            $j('#s').val(txt.replace(/"/g, ''));

    }
    }

</script>

<form action="/" id="searchform" method="get" role="search">
<div><label for="s" class="screen-reader-text">Search for:</label>
<input type="text" id="s" name="s" value="">
<input type="submit" value="Search" id="searchsubmit">
<input type="radio" name="sentence" value="" onclick="javascript:addquo();" /><label>All Words</label><br />
<input type="radio" name="sentence" value="???" checked="checked" onclick="javascript:removequo();" /><label>Exact Match</label>
</div>
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜