Search & Filter - Version 1.1.0

Version Description

  • Added support for checkboxes and radio buttons, with the option to control this for each individual taxonomy.
  • Added support to show or hide headings for each individual taxonomy.
  • Added support to pass a class name through to Search & Filter widgets, this allows styling of different instances of Search & Filter
  • Fixed problems with escaping output in search box
  • Notice: This update will automatically add headings to taxonomy dropdowns, refer to usage and examples on how to disable them.
Download this release

Release Info

Developer DesignsAndCode
Plugin Icon 128x128 Search & Filter
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.3 to 1.1.0

Files changed (6) hide show
  1. admin/style.css +15 -0
  2. of-admin.php +1 -1
  3. of-list-table.php +48 -2
  4. readme.txt +39 -5
  5. search-filter.php +418 -105
  6. style.css +9 -3
admin/style.css CHANGED
@@ -38,4 +38,19 @@ pre code
38
  {
39
  background-color:#eeeeee;
40
  font-size:12px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  }
38
  {
39
  background-color:#eeeeee;
40
  font-size:12px;
41
+ }
42
+
43
+ .column-info
44
+ {
45
+ width:65%;
46
+ }
47
+
48
+ .column-options
49
+ {
50
+ width:15%;
51
+ }
52
+
53
+ pre .keyword
54
+ {
55
+ color:#dd1144;
56
  }
of-admin.php CHANGED
@@ -63,7 +63,7 @@
63
  </div>';
64
 
65
 
66
- echo "<h3>Variables</h3>";
67
 
68
  //display table
69
  $ofVarListTable = new OF_Variable_List_Table();
63
  </div>';
64
 
65
 
66
+ echo "<h3>Arguments</h3>";
67
 
68
  //display table
69
  $ofVarListTable = new OF_Variable_List_Table();
of-list-table.php CHANGED
@@ -112,19 +112,65 @@ class OF_Variable_List_Table extends WP_List_Table {
112
  "name" => "taxonomies",
113
  "defaultval" => "&nbsp;",
114
  "options" => "<em>Comma seperated list of any taxonomy names found in the Public Taxonomies table below.</em>",
115
- "info" => "Example using all your taxonomies (copy &amp; paste!):<pre><code class='string'>[searchandfilter taxonomies=\"".$fulltaxonomylist."\"]</code></pre>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  );
117
  $counter++;
118
 
119
  $this->taxonomy_data[] = array(
120
  "ID" => $counter,
121
- "name" => "seach",
122
  "defaultval" => "1",
123
  "options" => "0 - hide the search box<br />1 - display search box",
124
  "info" => "The search box is shown by default, ommit from shortcode unless you specifically want to hide it - then set it with a value of 0."
125
  );
126
  $counter++;
127
 
 
 
 
 
 
 
 
 
 
 
128
  $this->taxonomy_data[] = array(
129
  "ID" => $counter,
130
  "name" => "submitlabel",
112
  "name" => "taxonomies",
113
  "defaultval" => "&nbsp;",
114
  "options" => "<em>Comma seperated list of any taxonomy names found in the Public Taxonomies table below.</em>",
115
+ "info" => "Example using all your public taxonomies (copy &amp; paste!):<pre><code class='string'>[searchandfilter taxonomies=\"".$fulltaxonomylist."\"]</code></pre>"
116
+ );
117
+ $counter++;
118
+
119
+
120
+ $this->taxonomy_data[] = array(
121
+ "ID" => $counter,
122
+ "name" => "type",
123
+ "defaultval" => "select",
124
+ "options" => "<em>Comma seperated list of any of the types found below:</em><br /><br />select<br />checkbox<br />radio",
125
+ "info" => "The order of values in this comma seperated list needs to match the taxonomies list. <br /><br />To display categories, tags and post formats, as a `select` dropdown, radio buttons and checkboxes, we must put them in the order we need:
126
+ <br /><pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" type=\"select,checkbox,radio\"]</code></pre>
127
+ If any taxonomies are left unspecified they well default to `select` dropdowns:
128
+ <br /><pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" type=\"select,checkbox\"]</code></pre>
129
+ With this example using just \"select,checkbox\", the post format (being the third, not provided parameter) will be displayed as a `select` dropdown.<br /><br />
130
+
131
+ If the `type` argument is ommited completely all taxonomies will be displayed as `select` dropdowns."
132
+ );
133
+ $counter++;
134
+
135
+ $this->taxonomy_data[] = array(
136
+ "ID" => $counter,
137
+ "name" => "label",
138
+ "defaultval" => "name",
139
+ "options" => "0 - hide all labels<br /><br /> or <br /><br /><em>Comma seperated list of any of the types found below:</em><br /><br />name<br />singular_name<br />search_items<br />all_items<br /><em>*blank value</em>",
140
+ "info" => "This list works the same as the `type` example above.<br /><br />
141
+ The different values that can be used are taken directly from the labels within a taxonomy object - so make sure you set these in your taxonomies if you wish to use them below.
142
+ <br /><br />Examples:<br /><br />
143
+ <strong>Hide all labels:</strong>
144
+ <pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" label=\"0\"]</code></pre>
145
+ <strong>Mixture of different label types:</strong>
146
+ <pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" label=\"singular_name,search_items,all_items\"]</code></pre>
147
+ <strong>Hiding the label for category and tag, and set `name` for the post format:</strong>
148
+ <pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" label=\",,name\"]</code></pre>
149
+ *In this last example, a blank value (ie, comma's with no data in between) tells Search &amp; Filter to hide the label for the particular taxonomy.<br /><br />
150
+ If the `label` argument is ommited completely all labels will be shown by default and will be set to use the `name` label for a taxonomy.
151
+ "
152
  );
153
  $counter++;
154
 
155
  $this->taxonomy_data[] = array(
156
  "ID" => $counter,
157
+ "name" => "search",
158
  "defaultval" => "1",
159
  "options" => "0 - hide the search box<br />1 - display search box",
160
  "info" => "The search box is shown by default, ommit from shortcode unless you specifically want to hide it - then set it with a value of 0."
161
  );
162
  $counter++;
163
 
164
+ $this->taxonomy_data[] = array(
165
+ "ID" => $counter,
166
+ "name" => "class",
167
+ "defaultval" => "",
168
+ "options" => "<em>Any string</em>",
169
+ "info" => "Enter a class name here, or class names seperated by spaces to have them added to Search &amp; Filter form. This allows individual styling of each Search &amp; Filter instance.<br /><br />Ommit to ignore."
170
+ );
171
+ $counter++;
172
+
173
+
174
  $this->taxonomy_data[] = array(
175
  "ID" => $counter,
176
  "name" => "submitlabel",
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === Search & Filter ===
2
  Contributors: DesignsAndCode
3
  Donate link:
4
- Tags: wordpress, category, filter, taxonomy, search
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
- Stable tag: 1.0.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -21,20 +21,54 @@ For basic usage, you can also have a look at the [plugin homepage](http://www.de
21
 
22
  == Frequently Asked Questions ==
23
 
 
 
 
 
 
 
 
 
 
 
 
24
  == Screenshots ==
25
 
26
- 1. Example of Search & Filter when used in a widget
27
- 2. Example of Search & Filter embedded in the header
 
28
 
29
  == Changelog ==
30
 
 
 
 
 
 
 
 
31
  = 1.0.3 =
32
  * Added some documention & screenshots to plugin page
33
 
 
 
 
 
 
 
 
 
 
 
34
  == Upgrade Notice ==
35
 
 
 
36
  == Description ==
37
 
38
  Search & Filter is a simple search and filtering plugin for WordPress. It is essentially an advancement of the WordPress search box, adding taxonomy filters to really refine your searches.
39
 
40
- You can search by Category, Tag, Custom Taxonomy or any combination of these easily you can even remove the search box and simply use it as a filtering system for your posts and pages.
 
 
 
1
  === Search & Filter ===
2
  Contributors: DesignsAndCode
3
  Donate link:
4
+ Tags: category, filter, taxonomy, search, wordpress
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
+ Stable tag: 1.1.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
21
 
22
  == Frequently Asked Questions ==
23
 
24
+ = Some text/headings/labels for my different Taxonomies seem to be broken or don't appear correctly =
25
+
26
+ Search & Filter uses the Taxonomy object and its label properly, this means when registering custom taxonomies make sure you fill out valid values for the following labels:
27
+
28
+ * name
29
+ * singular_name
30
+ * search_items
31
+ * all_items
32
+
33
+ If you have used a plugin to register a custom taxonomy this info can normally be found under advanced settings.
34
+
35
  == Screenshots ==
36
 
37
+ 1. Full example of Search & Filter when used in a widget and with a combination of checkboxes, radio buttons and selects
38
+ 2. Minimal example of Search & Filter embedded in the header
39
+ 3. Minimal example of Search & Filter embedded in a widget
40
 
41
  == Changelog ==
42
 
43
+ = 1.1.0 =
44
+ * Added support for checkboxes and radio buttons, with the option to control this for each individual taxonomy.
45
+ * Added support to show or hide headings for each individual taxonomy.
46
+ * Added support to pass a class name through to Search & Filter widgets, this allows styling of different instances of Search & Filter
47
+ * Fixed problems with escaping output in search box
48
+ * Notice: This update will automatically add headings to taxonomy dropdowns, refer to usage and examples on how to disable them.
49
+
50
  = 1.0.3 =
51
  * Added some documention & screenshots to plugin page
52
 
53
+ = 1.0.2 =
54
+ * Version bump for WordPress plugins site
55
+
56
+ = 1.0.1 =
57
+ * Updated to use `label->all_items` in taxonomy object for dropdowns before using `label->name`
58
+ * Notice: This update may cause some labels to break, ensure you have set up your taxonomy properly including setting `label->all_items`
59
+
60
+ = 1.0.0 =
61
+ * Initial Release
62
+
63
  == Upgrade Notice ==
64
 
65
+ Upgrade should be fully compatible with previous versions, however this update will automatically add headings to taxonomy dropdowns, refer to usage and examples on how to disable them.
66
+
67
  == Description ==
68
 
69
  Search & Filter is a simple search and filtering plugin for WordPress. It is essentially an advancement of the WordPress search box, adding taxonomy filters to really refine your searches.
70
 
71
+ You can search by Category, Tag, Custom Taxonomy or any combination of these easily - you can even remove the search box and simply use it as a filtering system for your posts and pages. Taxonomies can be displayed as dropdown selects, checkboxes or radio buttons.
72
+
73
+ For advanced usage & examples head over to the [plugin homepage](http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/).
74
+
search-filter.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.designsandcode.com/447/wordpress-search-filter-plugin-for
5
  Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies
6
  Author: Designs & Code
7
  Author URI: http://www.designsandcode.com/
8
- Version: 1.0.3
9
  Text Domain: searchandfilter
10
  License: GPLv2
11
  */
@@ -16,7 +16,7 @@ License: GPLv2
16
  * Set up Plugin Globals
17
  */
18
  if (!defined('SEARCHANDFILTER_VERSION_NUM'))
19
- define('SEARCHANDFILTER_VERSION_NUM', '1.0.3');
20
 
21
  if (!defined('SEARCHANDFILTER_THEME_DIR'))
22
  define('SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
@@ -65,6 +65,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
65
  {
66
  // Set up reserved taxonomies
67
  $this->frmreserved = array(SEARCHANDFILTER_FPRE."category", SEARCHANDFILTER_FPRE."search", SEARCHANDFILTER_FPRE."post_tag", SEARCHANDFILTER_FPRE."submitted");
 
68
 
69
  // Add shortcode support for widgets
70
  add_shortcode('searchandfilter', array($this, 'shortcode'));
@@ -99,92 +100,213 @@ if ( ! class_exists( 'SearchAndFilter' ) )
99
  extract(shortcode_atts(array(
100
  'search' => 1,
101
  'taxonomies' => null,
102
- 'submitlabel' => "Submit"
 
 
 
103
  ), $atts));
 
 
104
  $taxonomies = explode(",",$taxonomies);
105
  $this->taxonomylist = $taxonomies;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  //set all form defaults / dropdowns etc
108
  $this->set_defaults();
109
-
110
- return $this->get_search_filter_form($search, $submitlabel, $taxonomies);
111
  }
112
 
113
  /*
114
  * check to set defaults - to be called after the shortcodes have been init so we can grab the wanted list of taxonomies
115
  */
116
-
117
  public function set_defaults()
118
  {
119
- //var_dump( $this->taxonomylist);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
- if(is_category())
122
- {
123
- $category = get_category( get_query_var( 'cat' ) );
124
- $this->defaults[SEARCHANDFILTER_FPRE.'category'] = $category->cat_ID;
125
- }
126
- else
127
- {
128
- $this->defaults[SEARCHANDFILTER_FPRE.'category'] = 0;
129
- }
130
 
131
  //grab search term for prefilling search input
132
- if(isset($_GET['s']))
133
- {
134
- $this->searchterm = esc_attr($_GET['s']);
135
  }
136
 
137
  //check to see if tag is set
138
- if(isset($_GET['tag']))
139
- {//Else check the URL for the tag attribute
140
- $tagslug = esc_attr($_GET['tag']);
141
- $tagobj = get_term_by('slug',$tagslug,'post_tag');
142
- $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = 0;
143
- if(isset($tagobj->term_id))
144
- {
145
- $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = $tagobj->term_id;
146
- }
147
- }
148
- else
149
- {//else it is still possible that the URL could be website.com/tag/testtag -
150
- if(is_tag())
151
- {
152
- $tag = get_term_by("slug",get_query_var( 'tag' ), "post_tag");
153
- $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = $tag->term_id;
154
- }
155
- else
156
  {
157
- $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = 0;
 
 
 
 
 
 
 
 
 
158
  }
159
- }
 
160
 
161
- //loop through all the gets
162
- foreach($_GET as $key=>$val)
163
  {
164
- if(!in_array(SEARCHANDFILTER_FPRE.$key, $this->frmreserved))
165
  {//make sure the get is not a reserved get as they have already been handled above
166
 
167
  //now check it is a desired key
168
  if(in_array($key, $this->taxonomylist))
169
  {
170
- $taxslug = esc_attr($val);
171
- $taxobj = get_term_by('slug',$taxslug,$key);
172
- $this->defaults[SEARCHANDFILTER_FPRE.$key] = 0;
173
- if(isset($taxobj->term_id))
174
  {
175
- $this->defaults[SEARCHANDFILTER_FPRE.$key] = $taxobj->term_id;
 
 
 
 
 
176
  }
 
 
177
  }
178
  }
179
  }
180
 
 
181
  //now we may be on a taxonomy page
182
- if(is_tax())
183
  {
184
  $taxobj = get_queried_object();
185
  $taxid = $taxobj->term_id;
186
  $this->defaults[SEARCHANDFILTER_FPRE.$taxobj->taxonomy] = $taxobj->term_id;
187
- }
188
 
189
  }
190
 
@@ -206,26 +328,44 @@ if ( ! class_exists( 'SearchAndFilter' ) )
206
  /* CATEGORIES */
207
  if((isset($_POST[SEARCHANDFILTER_FPRE.'category']))&&($this->has_search_posted))
208
  {
209
- $this->defaults[SEARCHANDFILTER_FPRE.'category'] = esc_attr($_POST[SEARCHANDFILTER_FPRE.'category']);
210
- $catobj = get_category($this->defaults[SEARCHANDFILTER_FPRE.'category']);
211
 
212
- if(isset($catobj->slug))
 
213
  {
214
- // deal with category firstm so we can build a url like:
215
- // site.com/category/products/?tag=atag&s=searchterm
216
- // rather than:
217
- // site.com/?category_name=products&tag=atag&s=searchterm
 
 
 
 
 
 
 
 
218
 
 
 
 
 
 
 
 
 
 
 
 
219
  if(get_option('permalink_structure'))
220
- {//if has permalinks use nice formatting as above
221
-
222
- $catrel = trim(str_replace(home_url(), "", get_category_link( $catobj->term_id )), "/")."/"; //get full category link, remvoe the home url to get relative, trim traling slashed, the append slash at the end
223
- $this->urlparams .= $catrel;// old - not reliable - "category/".$catobj->slug."/";
224
-
225
  }
226
  else
227
- {//otherwise stick everything in to the query string and let wp deal with it
228
-
229
  if(!$this->hasqmark)
230
  {
231
  $this->urlparams .= "?";
@@ -235,8 +375,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
235
  {
236
  $this->urlparams .= "&";
237
  }
238
-
239
- $this->urlparams .= "category_name=".$catobj->slug;
240
  }
241
  }
242
  }
@@ -244,7 +383,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
244
  /* SEARCH BOX */
245
  if((isset($_POST[SEARCHANDFILTER_FPRE.'search']))&&($this->has_search_posted))
246
  {
247
- $this->searchterm = urlencode($_POST[SEARCHANDFILTER_FPRE.'search']);
248
 
249
  if($this->searchterm!="")
250
  {
@@ -257,17 +396,41 @@ if ( ! class_exists( 'SearchAndFilter' ) )
257
  {
258
  $this->urlparams .= "&";
259
  }
260
- $this->urlparams .= "s=".$this->searchterm;
261
  }
262
  }
263
 
264
  /* TAGS */
265
  if((isset($_POST[SEARCHANDFILTER_FPRE.'post_tag']))&&($this->has_search_posted))
266
- {//If the search form has been submitted with a new tag filter
267
- $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = esc_attr($_POST[SEARCHANDFILTER_FPRE.'post_tag']);
268
- $tagobj = get_tag($this->defaults[SEARCHANDFILTER_FPRE.'post_tag']);
269
- if(isset($tagobj->slug))
 
270
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  if(!$this->hasqmark)
272
  {
273
  $this->urlparams .= "?";
@@ -277,20 +440,20 @@ if ( ! class_exists( 'SearchAndFilter' ) )
277
  {
278
  $this->urlparams .= "&";
279
  }
280
- $this->urlparams .= "tag=".$tagobj->slug;
 
281
  }
282
  }
283
 
284
-
285
  //now we have dealt with the all the special case variables - search, tags, categories
286
 
287
  /* TAXONOMIES */
288
  //loop through the posts - double check that it is the search form that has been posted, otherwise we could be looping through the posts submitted from an entirely unrelated form
289
  foreach($_POST as $key=>$val)
290
  {
 
291
  if(!in_array($key, $this->frmreserved))
292
  {//if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term)
293
- //echo ($key).": ".$val."<br />";
294
 
295
  // strip off all prefixes for custom taxonomies - we just want to do a redirect - no processing
296
  if (strpos($key, SEARCHANDFILTER_FPRE) === 0)
@@ -298,11 +461,34 @@ if ( ! class_exists( 'SearchAndFilter' ) )
298
  $key = substr($key, strlen(SEARCHANDFILTER_FPRE));
299
  }
300
 
301
- $temptax = esc_attr($val);
302
- $taxobj = get_term_by('id',$temptax,$key);
303
 
304
- if(isset($taxobj->slug))
 
305
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  if(!$this->hasqmark)
307
  {
308
  $this->urlparams .= "?";
@@ -312,68 +498,92 @@ if ( ! class_exists( 'SearchAndFilter' ) )
312
  {
313
  $this->urlparams .= "&";
314
  }
315
-
316
- $this->urlparams .= $key."=".$taxobj->slug;
317
  }
318
-
319
-
320
- //$this->defaults[$key] = $val;
321
- //echo $key.": ".$val;
322
-
323
  }
324
  }
325
 
326
  if($this->has_search_posted)
327
  {//if the search has been posted, redirect to the newly formed url with all the right params
328
 
329
- wp_redirect( site_url().$this->urlparams );
330
  }
331
-
332
  }
333
 
334
- public function get_search_filter_form($search, $submitlabel, $taxonomies)
335
- {
336
- //ob_start();
337
  $returnvar = '';
338
 
 
 
 
 
 
 
339
  $returnvar .= '
340
- <form action="" method="post" class="searchandfilter">
341
- <p>
342
  <ul>';
343
 
344
  if($search==1)
345
  {
346
- $returnvar .= '<li><input type="text" name="ofsearch" placeholder="Search &hellip;" value="'.$this->searchterm.'"></li>';
 
 
 
347
  }
348
 
 
 
349
  foreach($taxonomies as $taxonomy)
350
  {
351
 
352
  $taxonomydata = get_taxonomy($taxonomy);
353
- //var_dump($taxonomydata);
354
  if($taxonomydata)
355
  {
356
  $returnvar .= "<li>";
 
 
 
 
 
 
357
  $taxonomychildren = get_categories('name=of'.$taxonomy.'&taxonomy='.$taxonomy);
358
- $returnvar .= $this->generate_dropdown($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
 
 
 
 
 
 
 
 
 
 
 
 
359
  $returnvar .= "</li>";
360
  }
 
361
 
362
  }
363
-
364
- $returnvar .= "</ul>";
365
-
366
  $returnvar .=
367
- '<p>
368
  <input type="hidden" name="'.SEARCHANDFILTER_FPRE.'submitted" value="1">
369
  <input type="submit" value="'.$submitlabel.'">
370
- </p>
371
- </p>
 
 
372
  </form>';
373
 
374
  return $returnvar;
375
  }
376
- public function generate_dropdown($dropdata, $name, $currentid = 0, $labels = null)
377
  {
378
  $returnvar = "";
379
 
@@ -381,11 +591,11 @@ if ( ! class_exists( 'SearchAndFilter' ) )
381
  if(isset($labels))
382
  {
383
  if($labels->all_items!="")
384
- {
385
  $returnvar .= '<option class="level-0" value="0">'.$labels->all_items.'</option>';
386
  }
387
  else
388
- {
389
  $returnvar .= '<option class="level-0" value="0">All '.$labels->name.'</option>';
390
  }
391
  }
@@ -393,11 +603,22 @@ if ( ! class_exists( 'SearchAndFilter' ) )
393
  foreach($dropdata as $dropdown)
394
  {
395
  $selected = "";
 
396
  if(isset($this->defaults[SEARCHANDFILTER_FPRE.$name]))
397
  {
398
- if($this->defaults[SEARCHANDFILTER_FPRE.$name]==$dropdown->term_id)
 
 
 
 
399
  {
400
- $selected = ' selected="selected"';
 
 
 
 
 
 
401
  }
402
  }
403
  $returnvar .= '<option class="level-0" value="'.$dropdown->term_id.'"'.$selected.'>'.$dropdown->cat_name.'</option>';
@@ -405,6 +626,98 @@ if ( ! class_exists( 'SearchAndFilter' ) )
405
  }
406
  $returnvar .= "</select>";
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  return $returnvar;
409
  }
410
  }
5
  Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies
6
  Author: Designs & Code
7
  Author URI: http://www.designsandcode.com/
8
+ Version: 1.1.0
9
  Text Domain: searchandfilter
10
  License: GPLv2
11
  */
16
  * Set up Plugin Globals
17
  */
18
  if (!defined('SEARCHANDFILTER_VERSION_NUM'))
19
+ define('SEARCHANDFILTER_VERSION_NUM', '1.1.0');
20
 
21
  if (!defined('SEARCHANDFILTER_THEME_DIR'))
22
  define('SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
65
  {
66
  // Set up reserved taxonomies
67
  $this->frmreserved = array(SEARCHANDFILTER_FPRE."category", SEARCHANDFILTER_FPRE."search", SEARCHANDFILTER_FPRE."post_tag", SEARCHANDFILTER_FPRE."submitted");
68
+ $this->frmqreserved = array(SEARCHANDFILTER_FPRE."category_name", SEARCHANDFILTER_FPRE."s", SEARCHANDFILTER_FPRE."tag", SEARCHANDFILTER_FPRE."submitted"); //same as reserved
69
 
70
  // Add shortcode support for widgets
71
  add_shortcode('searchandfilter', array($this, 'shortcode'));
100
  extract(shortcode_atts(array(
101
  'search' => 1,
102
  'taxonomies' => null,
103
+ 'submitlabel' => "Submit",
104
+ 'type' => "",
105
+ 'label' => "default",
106
+ 'class' => ""
107
  ), $atts));
108
+
109
+
110
  $taxonomies = explode(",",$taxonomies);
111
  $this->taxonomylist = $taxonomies;
112
+ $notaxonomies = count($taxonomies);
113
+
114
+ //set default types for each taxonomy
115
+ $types = explode(",",$type);
116
+ if(!is_array($types))
117
+ {
118
+ $types = array();
119
+ }
120
+
121
+ $labels = explode(",",$label);
122
+ $showlabels = true;
123
+ $labeldefault = "name";
124
+
125
+ if(!is_array($labels))
126
+ {
127
+ $labels = array("default");
128
+ }
129
+
130
+ for($i=0; $i<$notaxonomies; $i++)
131
+ {//loop through all taxonomies
132
+
133
+ //set up types
134
+ if(isset($types[$i]))
135
+ {
136
+
137
+ if(($types[$i]=="select")||($types[$i]=="checkbox")||($types[$i]=="radio"))
138
+ {
139
+ $types[$i] = $types[$i];
140
+ }
141
+ else
142
+ {
143
+ $types[$i] = "select";
144
+ }
145
+ }
146
+ else
147
+ {
148
+ $types[$i] = "select";
149
+ }
150
+
151
+
152
+
153
+ if(isset($labels[0]))
154
+ {//these means at least one option has been set
155
+
156
+ if(($labels[0]=="0")||($labels[0]=="none"))
157
+ {
158
+ if($i!=0)
159
+ {
160
+ $labels[$i] = ""; //then set all fields to blank ("no label").
161
+ }
162
+ }
163
+ else if($labels[0]=="default")
164
+ {
165
+ $labels[$i] = $labeldefault;
166
+ }
167
+ else
168
+ {//then one or more options were passed, and the value wasn't "0" or "none"
169
+
170
+ if(isset($labels[$i]))
171
+ {
172
+ if(($labels[$i]=="name")||($labels[$i]=="singular_name")||($labels[$i]=="search_items")||($labels[$i]=="all_items"))
173
+ {//enforce the use of only above mentioned labels, don't support the other ones or just any data supplied by the user
174
+
175
+ $labels[$i] = $labels[$i];
176
+ }
177
+ else if($labels[$i]=="")
178
+ {//if it is blank no label should be shown for this taxonomy
179
+
180
+ $labels[$i] = "";
181
+ }
182
+ else
183
+ {//this normally means a typos or unrecognised type, so use default
184
+
185
+ $labels[$i] = $labeldefault;
186
+ }
187
+ }
188
+ else
189
+ {
190
+ $labels[$i] = $labeldefault;
191
+ }
192
+ }
193
+ }
194
+ else
195
+ {//then it has been completely omitted so use default display
196
+ $labels[$i] = $labeldefault;
197
+ }
198
+
199
+
200
+ }
201
+
202
+ //if we have a value of 0 or "none" in teh first part of the array, the we need to set it to blanks so it doesn't display
203
+ if(isset($labels[0]))
204
+ {
205
+ if(($labels[0]=="0")||($labels[0]=="none"))
206
+ {
207
+ $labels[0] = ""; //then set all fields to blank ("no label").
208
+ }
209
+ }
210
 
211
  //set all form defaults / dropdowns etc
212
  $this->set_defaults();
213
+
214
+ return $this->get_search_filter_form($search, $submitlabel, $taxonomies, $types, $labels, $class);
215
  }
216
 
217
  /*
218
  * check to set defaults - to be called after the shortcodes have been init so we can grab the wanted list of taxonomies
219
  */
 
220
  public function set_defaults()
221
  {
222
+ global $wp_query;
223
+ /*var_dump($wp_query->query['category_name']);
224
+ var_dump($wp_query->query['tag']);*/
225
+ //var_dump($wp_query->query);
226
+
227
+ $categories = array();
228
+ //if(is_category())
229
+ //{
230
+ if(isset($wp_query->query['category_name']))
231
+ {
232
+
233
+ $category_params = explode("+",esc_attr($wp_query->query['category_name']));
234
+
235
+ foreach($category_params as $category_param)
236
+ {
237
+ $category = get_category_by_slug( $category_param );
238
+ if(isset($category->cat_ID))
239
+ {
240
+ $categories[] = $category->cat_ID;
241
+ }
242
+ }
243
+ }
244
+ //}
245
+ $this->defaults[SEARCHANDFILTER_FPRE.'category'] = $categories;
246
 
 
 
 
 
 
 
 
 
 
247
 
248
  //grab search term for prefilling search input
249
+ if(isset($wp_query->query['s']))
250
+ {//!"�$%^&*()
251
+ $this->searchterm = get_search_query();
252
  }
253
 
254
  //check to see if tag is set
255
+
256
+ $tags = array();
257
+ //if(is_tag())
258
+ //{
259
+ if(isset($wp_query->query['tag']))
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  {
261
+ $tag_params = explode("+",esc_attr($wp_query->query['tag']));
262
+
263
+ foreach($tag_params as $tag_param)
264
+ {
265
+ $tag = get_term_by("slug",$tag_param, "post_tag");
266
+ if(isset($tag->term_id))
267
+ {
268
+ $tags[] = $tag->term_id;
269
+ }
270
+ }
271
  }
272
+ //}
273
+ $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = $tags;
274
 
275
+ //loop through all the query vars
276
+ foreach($wp_query->query as $key=>$val)
277
  {
278
+ if(!in_array(SEARCHANDFILTER_FPRE.$key, $this->frmqreserved))
279
  {//make sure the get is not a reserved get as they have already been handled above
280
 
281
  //now check it is a desired key
282
  if(in_array($key, $this->taxonomylist))
283
  {
284
+ $taxslug = ($val);
285
+ $tax_params = explode("+",esc_attr($taxslug));
286
+
287
+ foreach($tax_params as $tax_param)
288
  {
289
+ $tax = get_term_by("slug",$tax_param, $key);
290
+
291
+ if(isset($tax->term_id))
292
+ {
293
+ $taxs[] = $tax->term_id;
294
+ }
295
  }
296
+
297
+ $this->defaults[SEARCHANDFILTER_FPRE.$key] = $taxs;
298
  }
299
  }
300
  }
301
 
302
+
303
  //now we may be on a taxonomy page
304
+ /*if(is_tax())
305
  {
306
  $taxobj = get_queried_object();
307
  $taxid = $taxobj->term_id;
308
  $this->defaults[SEARCHANDFILTER_FPRE.$taxobj->taxonomy] = $taxobj->term_id;
309
+ }*/
310
 
311
  }
312
 
328
  /* CATEGORIES */
329
  if((isset($_POST[SEARCHANDFILTER_FPRE.'category']))&&($this->has_search_posted))
330
  {
331
+ $the_post_cat = ($_POST[SEARCHANDFILTER_FPRE.'category']);
 
332
 
333
+ //make the post an array for easy looping
334
+ if(!is_array($_POST[SEARCHANDFILTER_FPRE.'category']))
335
  {
336
+ $post_cat[] = $the_post_cat;
337
+ }
338
+ else
339
+ {
340
+ $post_cat = $the_post_cat;
341
+ }
342
+ $catarr = array();
343
+
344
+ foreach ($post_cat as $cat)
345
+ {
346
+ $cat = esc_attr($cat);
347
+ $catobj = get_category($cat);
348
 
349
+ if(isset($catobj->slug))
350
+ {
351
+ $catarr[] = $catobj->slug;
352
+ //$catarr[] = $catobj->term_id;
353
+ }
354
+ }
355
+
356
+ if(count($catarr)>0)
357
+ {
358
+ $categories = implode("+",$catarr);
359
+
360
  if(get_option('permalink_structure'))
361
+ {
362
+ //$catrel = trim(str_replace(home_url(), "", get_category_link()), "/").$categories."/"; //get full category link, remvoe the home url to get relative, trim traling slashed, the append slash at the end
363
+ $category_base = (get_option( 'category_base' )=="") ? "category" : get_option( 'category_base' );
364
+ $category_path = $category_base."/".$categories."/";
365
+ $this->urlparams .= $category_path;
366
  }
367
  else
368
+ {
 
369
  if(!$this->hasqmark)
370
  {
371
  $this->urlparams .= "?";
375
  {
376
  $this->urlparams .= "&";
377
  }
378
+ $this->urlparams .= "category_name=".$categories;
 
379
  }
380
  }
381
  }
383
  /* SEARCH BOX */
384
  if((isset($_POST[SEARCHANDFILTER_FPRE.'search']))&&($this->has_search_posted))
385
  {
386
+ $this->searchterm = stripslashes($_POST[SEARCHANDFILTER_FPRE.'search']);
387
 
388
  if($this->searchterm!="")
389
  {
396
  {
397
  $this->urlparams .= "&";
398
  }
399
+ $this->urlparams .= "s=".urlencode($this->searchterm);
400
  }
401
  }
402
 
403
  /* TAGS */
404
  if((isset($_POST[SEARCHANDFILTER_FPRE.'post_tag']))&&($this->has_search_posted))
405
+ {
406
+ $the_post_tag = ($_POST[SEARCHANDFILTER_FPRE.'post_tag']);
407
+
408
+ //make the post an array for easy looping
409
+ if(!is_array($_POST[SEARCHANDFILTER_FPRE.'post_tag']))
410
  {
411
+ $post_tag[] = $the_post_tag;
412
+ }
413
+ else
414
+ {
415
+ $post_tag = $the_post_tag;
416
+ }
417
+ $tagarr = array();
418
+
419
+ foreach ($post_tag as $tag)
420
+ {
421
+ $tag = esc_attr($tag);
422
+ $tagobj = get_tag($tag);
423
+
424
+ if(isset($tagobj->slug))
425
+ {
426
+ $tagarr[] = $tagobj->slug;
427
+ }
428
+ }
429
+
430
+ if(count($tagarr)>0)
431
+ {
432
+ $tags = implode("+",$tagarr);
433
+
434
  if(!$this->hasqmark)
435
  {
436
  $this->urlparams .= "?";
440
  {
441
  $this->urlparams .= "&";
442
  }
443
+ $this->urlparams .= "tag=".$tags;
444
+
445
  }
446
  }
447
 
 
448
  //now we have dealt with the all the special case variables - search, tags, categories
449
 
450
  /* TAXONOMIES */
451
  //loop through the posts - double check that it is the search form that has been posted, otherwise we could be looping through the posts submitted from an entirely unrelated form
452
  foreach($_POST as $key=>$val)
453
  {
454
+
455
  if(!in_array($key, $this->frmreserved))
456
  {//if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term)
 
457
 
458
  // strip off all prefixes for custom taxonomies - we just want to do a redirect - no processing
459
  if (strpos($key, SEARCHANDFILTER_FPRE) === 0)
461
  $key = substr($key, strlen(SEARCHANDFILTER_FPRE));
462
  }
463
 
464
+ $the_post_tax = $val;
 
465
 
466
+ //make the post an array for easy looping
467
+ if(!is_array($val))
468
  {
469
+ $post_tax[] = $the_post_tax;
470
+ }
471
+ else
472
+ {
473
+ $post_tax = $the_post_tax;
474
+ }
475
+ $taxarr = array();
476
+
477
+ foreach ($post_tax as $tax)
478
+ {
479
+ $tax = esc_attr($tax);
480
+ $taxobj = get_term_by('id',$tax,$key);
481
+
482
+ if(isset($taxobj->slug))
483
+ {
484
+ $taxarr[] = $taxobj->slug;
485
+ }
486
+ }
487
+
488
+ if(count($taxarr)>0)
489
+ {
490
+ $tags = implode("+",$taxarr);
491
+
492
  if(!$this->hasqmark)
493
  {
494
  $this->urlparams .= "?";
498
  {
499
  $this->urlparams .= "&";
500
  }
501
+ $this->urlparams .= $key."=".$tags;
502
+
503
  }
 
 
 
 
 
504
  }
505
  }
506
 
507
  if($this->has_search_posted)
508
  {//if the search has been posted, redirect to the newly formed url with all the right params
509
 
510
+ wp_redirect( (home_url().$this->urlparams) );
511
  }
512
+
513
  }
514
 
515
+ public function get_search_filter_form($search, $submitlabel, $taxonomies, $types, $labels, $class)
516
+ {
 
517
  $returnvar = '';
518
 
519
+ $addclass = "";
520
+ if($class!="")
521
+ {
522
+ $addclass = ' '.$class;
523
+ }
524
+
525
  $returnvar .= '
526
+ <form action="" method="post" class="searchandfilter'.$addclass.'">
527
+ <div>
528
  <ul>';
529
 
530
  if($search==1)
531
  {
532
+
533
+ $clean_searchterm = (esc_attr($this->searchterm));
534
+
535
+ $returnvar .= '<li><input type="text" name="ofsearch" placeholder="Search &hellip;" value="'.$clean_searchterm.'"></li>';
536
  }
537
 
538
+ $i = 0;
539
+
540
  foreach($taxonomies as $taxonomy)
541
  {
542
 
543
  $taxonomydata = get_taxonomy($taxonomy);
544
+
545
  if($taxonomydata)
546
  {
547
  $returnvar .= "<li>";
548
+
549
+ if($labels[$i]!="")
550
+ {
551
+ $returnvar .= "<h4>".$taxonomydata->labels->{$labels[$i]}."</h4>";
552
+ }
553
+
554
  $taxonomychildren = get_categories('name=of'.$taxonomy.'&taxonomy='.$taxonomy);
555
+
556
+ if($types[$i]=="select")
557
+ {
558
+ $returnvar .= $this->generate_select($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
559
+ }
560
+ else if($types[$i]=="checkbox")
561
+ {
562
+ $returnvar .= $this->generate_checkbox($taxonomychildren, $taxonomy, $this->tagid);
563
+ }
564
+ else if($types[$i]=="radio")
565
+ {
566
+ $returnvar .= $this->generate_radio($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
567
+ }
568
  $returnvar .= "</li>";
569
  }
570
+ $i++;
571
 
572
  }
573
+
 
 
574
  $returnvar .=
575
+ '<li>
576
  <input type="hidden" name="'.SEARCHANDFILTER_FPRE.'submitted" value="1">
577
  <input type="submit" value="'.$submitlabel.'">
578
+ </li>';
579
+
580
+ $returnvar .= "</ul>";
581
+ $returnvar .= '</div>
582
  </form>';
583
 
584
  return $returnvar;
585
  }
586
+ public function generate_select($dropdata, $name, $currentid = 0, $labels = null)
587
  {
588
  $returnvar = "";
589
 
591
  if(isset($labels))
592
  {
593
  if($labels->all_items!="")
594
+ {//check to see if all items has been registered in taxonomy then use this label
595
  $returnvar .= '<option class="level-0" value="0">'.$labels->all_items.'</option>';
596
  }
597
  else
598
+ {//check to see if all items has been registered in taxonomy then use this label with prefix of "All"
599
  $returnvar .= '<option class="level-0" value="0">All '.$labels->name.'</option>';
600
  }
601
  }
603
  foreach($dropdata as $dropdown)
604
  {
605
  $selected = "";
606
+
607
  if(isset($this->defaults[SEARCHANDFILTER_FPRE.$name]))
608
  {
609
+ $defaults = $this->defaults[SEARCHANDFILTER_FPRE.$name];
610
+
611
+ $noselected = count($defaults);
612
+
613
+ if(($noselected>0)&&(is_array($defaults)))
614
  {
615
+ foreach($defaults as $defaultid)
616
+ {
617
+ if($defaultid==$dropdown->term_id)
618
+ {
619
+ $selected = ' selected="selected"';
620
+ }
621
+ }
622
  }
623
  }
624
  $returnvar .= '<option class="level-0" value="'.$dropdown->term_id.'"'.$selected.'>'.$dropdown->cat_name.'</option>';
626
  }
627
  $returnvar .= "</select>";
628
 
629
+ return $returnvar;
630
+ }
631
+ public function generate_checkbox($dropdata, $name, $currentid = 0, $labels = null)
632
+ {
633
+ $returnvar = "";
634
+
635
+ foreach($dropdata as $dropdown)
636
+ {
637
+ $checked = "";
638
+
639
+ //check a default has been set
640
+ if(isset($this->defaults[SEARCHANDFILTER_FPRE.$name]))
641
+ {
642
+ $defaults = $this->defaults[SEARCHANDFILTER_FPRE.$name];
643
+
644
+ $noselected = count($defaults);
645
+
646
+ if(($noselected>0)&&(is_array($defaults)))
647
+ {
648
+ foreach($defaults as $defaultid)
649
+ {
650
+ if($defaultid==$dropdown->term_id)
651
+ {
652
+ $checked = ' checked="checked"';
653
+ }
654
+ }
655
+ }
656
+ }
657
+ $returnvar .= '<label><input class="postform" type="checkbox" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="'.$dropdown->term_id.'"'.$checked.'> '.$dropdown->cat_name.'</label>';
658
+
659
+ }
660
+
661
+ return $returnvar;
662
+ }
663
+
664
+ public function generate_radio($dropdata, $name, $currentid = 0, $labels = null)
665
+ {
666
+ $returnvar = "";
667
+
668
+ if(isset($labels))
669
+ {
670
+ $checked = "";
671
+ if(isset($this->defaults[SEARCHANDFILTER_FPRE.$name]))
672
+ {
673
+ $defaults = $this->defaults[SEARCHANDFILTER_FPRE.$name];
674
+ $noselected = count($defaults);
675
+
676
+ if($noselected==0)
677
+ {
678
+ $checked = ' checked="checked"';
679
+ }
680
+ }
681
+ else
682
+ {
683
+ $checked = ' checked="checked"';
684
+ }
685
+ if($labels->all_items!="")
686
+ {//check to see if all items has been registered in taxonomy then use this label
687
+ $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="0"'.$checked.'> '.$labels->all_items.'</label>';
688
+ }
689
+ else
690
+ {//check to see if all items has been registered in taxonomy then use this label with prefix of "All"
691
+ $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="0"'.$checked.'> '.$labels->name.'</label>';
692
+ }
693
+ }
694
+
695
+ foreach($dropdata as $dropdown)
696
+ {
697
+ $checked = "";
698
+
699
+ //check a default has been set
700
+ if(isset($this->defaults[SEARCHANDFILTER_FPRE.$name]))
701
+ {
702
+ $defaults = $this->defaults[SEARCHANDFILTER_FPRE.$name];
703
+
704
+ $noselected = count($defaults);
705
+
706
+ if(($noselected>0)&&(is_array($defaults)))
707
+ {
708
+ foreach($defaults as $defaultid)
709
+ {
710
+ if($defaultid==$dropdown->term_id)
711
+ {
712
+ $checked = ' checked="checked"';
713
+ }
714
+ }
715
+ }
716
+ }
717
+ $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="'.$dropdown->term_id.'"'.$checked.'> '.$dropdown->cat_name.'</label>';
718
+
719
+ }
720
+
721
  return $returnvar;
722
  }
723
  }
style.css CHANGED
@@ -1,16 +1,13 @@
1
  /* Search & Filter Styles */
2
-
3
  .searchandfilter p
4
  {
5
  margin-top: 1em;
6
  display:inline-block;
7
  }
8
-
9
  .searchandfilter ul
10
  {
11
  display:inline-block;
12
  }
13
-
14
  .searchandfilter li
15
  {
16
  list-style: none;
@@ -23,3 +20,12 @@
23
  {
24
  display:block;
25
  }
 
 
 
 
 
 
 
 
 
1
  /* Search & Filter Styles */
 
2
  .searchandfilter p
3
  {
4
  margin-top: 1em;
5
  display:inline-block;
6
  }
 
7
  .searchandfilter ul
8
  {
9
  display:inline-block;
10
  }
 
11
  .searchandfilter li
12
  {
13
  list-style: none;
20
  {
21
  display:block;
22
  }
23
+ .searchandfilter label
24
+ {
25
+ display:block;
26
+ }
27
+ .searchandfilter h4
28
+ {
29
+ margin:15px 0;
30
+ font-size:16px;
31
+ }