Custom Taxonomy Order NE - Version 3.3.3

Version Description

  • 2022-01-24
  • Traverse all parents for sorting child terms, not just the toplevel ancestor.
  • Some fixes on admin page for sorting subterms.
  • Add filter 'customtaxorder_settings'.
Download this release

Release Info

Developer mpol
Plugin Icon 128x128 Custom Taxonomy Order NE
Version 3.3.3
Comparing to
See all releases

Code changes from version 3.3.2 to 3.3.3

Files changed (3) hide show
  1. customtaxorder.php +28 -14
  2. page-customtaxorder.php +7 -6
  3. readme.txt +48 -9
customtaxorder.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Custom Taxonomy Order
4
  Plugin URI: https://wordpress.org/plugins/custom-taxonomy-order-ne/
5
  Description: Allows for the ordering of categories and custom taxonomy terms through a simple drag-and-drop interface.
6
- Version: 3.3.2
7
  Author: Marcel Pol
8
  Author URI: https://timelord.nl/
9
  License: GPLv2 or later
@@ -12,7 +12,7 @@ Domain Path: /lang/
12
 
13
 
14
  Copyright 2011 - 2011 Drew Gourley
15
- Copyright 2013 - 2021 Marcel Pol (marcel@timelord.nl)
16
 
17
  This program is free software; you can redistribute it and/or
18
  modify it under the terms of the GNU General Public License
@@ -40,12 +40,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40
 
41
 
42
  // Plugin Version
43
- define('CUSTOMTAXORDER_VER', '3.3.1');
44
 
45
 
46
  /*
47
- * Get settings for ordering this taxonomy.
48
- * $customtaxorder_settings is an array with key: $taxonomy->name and value: setting (0, 1, 2).
 
 
 
 
 
 
49
  */
50
  function customtaxorder_get_settings() {
51
  $customtaxorder_defaults = array('category' => 0);
@@ -59,6 +65,8 @@ function customtaxorder_get_settings() {
59
  $customtaxorder_settings = get_option( 'customtaxorder_settings' );
60
  $customtaxorder_settings = wp_parse_args( $customtaxorder_settings, $customtaxorder_defaults );
61
 
 
 
62
  return $customtaxorder_settings;
63
  }
64
 
@@ -196,22 +204,28 @@ function customtaxorder_wp_get_object_terms_order_filter( $terms ) {
196
  }
197
  }
198
 
199
- // Sort children after the ancestor, by using a float with "ancestor.child".
200
  foreach ($terms as $term) {
201
  if ( ! $term->parent == 0 ) {
202
- $parents = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
203
- if ( is_array($parents) && ! empty($parents) ) {
204
- $ancestor_id = array_pop( $parents );
205
- $ancestor_term = get_term($ancestor_id, $term->taxonomy);
206
  if ( is_object($ancestor_term) && isset($ancestor_term->term_order) ) {
207
- $float_front = (string) $ancestor_term->term_order;
208
- $float_rear = (string) ( $term->term_order + 10000 ); // Make it sort correctly. Not many websites have more than 90000 subterms.
209
- $term->term_order = (float) ( $float_front . '.' . $float_rear );
 
 
 
 
 
 
 
210
  }
211
  }
212
  }
213
  }
214
-
215
  usort($terms, 'customtax_cmp');
216
 
217
  $terms_new_order = $terms;
3
  Plugin Name: Custom Taxonomy Order
4
  Plugin URI: https://wordpress.org/plugins/custom-taxonomy-order-ne/
5
  Description: Allows for the ordering of categories and custom taxonomy terms through a simple drag-and-drop interface.
6
+ Version: 3.3.3
7
  Author: Marcel Pol
8
  Author URI: https://timelord.nl/
9
  License: GPLv2 or later
12
 
13
 
14
  Copyright 2011 - 2011 Drew Gourley
15
+ Copyright 2013 - 2022 Marcel Pol (marcel@timelord.nl)
16
 
17
  This program is free software; you can redistribute it and/or
18
  modify it under the terms of the GNU General Public License
40
 
41
 
42
  // Plugin Version
43
+ define('CUSTOMTAXORDER_VER', '3.3.3');
44
 
45
 
46
  /*
47
+ * Get settings for ordering each taxonomy.
48
+ * Settings:
49
+ * 0: orderby Term ID
50
+ * 1: orderby Custom Order
51
+ * 2: orderby Term Name (alphabetical)
52
+ * 3: orderby Term Slug (alphabetical)
53
+ *
54
+ * @return array $customtaxorder_settings an array with key: $taxonomy->name and value:
55
  */
56
  function customtaxorder_get_settings() {
57
  $customtaxorder_defaults = array('category' => 0);
65
  $customtaxorder_settings = get_option( 'customtaxorder_settings' );
66
  $customtaxorder_settings = wp_parse_args( $customtaxorder_settings, $customtaxorder_defaults );
67
 
68
+ $customtaxorder_settings = apply_filters( 'customtaxorder_settings', $customtaxorder_settings );
69
+
70
  return $customtaxorder_settings;
71
  }
72
 
204
  }
205
  }
206
 
207
+ // Sort children after the ancestor, by using a float with "ancestor.childchildchild", traversing all parents.
208
  foreach ($terms as $term) {
209
  if ( ! $term->parent == 0 ) {
210
+ $ancestors = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
211
+ if ( is_array($ancestors) && ! empty($ancestors) ) {
212
+ $toplevel_ancestor_id = array_pop( $ancestors );
213
+ $ancestor_term = get_term($toplevel_ancestor_id, $term->taxonomy);
214
  if ( is_object($ancestor_term) && isset($ancestor_term->term_order) ) {
215
+ $front_of_float = (string) $ancestor_term->term_order;
216
+ $rear_of_float = '';
217
+ foreach ( $ancestors as $ancestor_id ) {
218
+ $ancestor_term = get_term($ancestor_id, $term->taxonomy);
219
+ if ( is_object($ancestor_term) && isset($ancestor_term->term_order) ) {
220
+ $rear_of_float .= (string) ($ancestor_term->term_order + 10000); // Make it sort correctly. Not many websites have more than 90000 subterms.
221
+ }
222
+ }
223
+ $rear_of_float .= (string) ($term->term_order + 10000);
224
+ $term->term_order = (float) ( $front_of_float . '.' . $rear_of_float );
225
  }
226
  }
227
  }
228
  }
 
229
  usort($terms, 'customtax_cmp');
230
 
231
  $terms_new_order = $terms;
page-customtaxorder.php CHANGED
@@ -10,7 +10,6 @@ function customtaxorder_subpage() {
10
 
11
  $options = customtaxorder_get_settings();
12
  $taxonomies = customtaxorder_get_taxonomies();
13
- $parent_id = 0;
14
  $this_page = sanitize_text_field( $_GET['page'] );
15
 
16
  // Set your custom capability through this filter.
@@ -104,15 +103,17 @@ function customtaxorder_subpage() {
104
  }
105
  }
106
 
 
107
  $parent_id_order = 0;
108
  if (isset($_POST['go-sub-posts'])) {
109
  $parent_id = (int) $_POST['sub-posts'];
110
  } else if (isset($_POST['hidden-parent-id'])) {
111
- $parent_term = get_term($_POST['hidden-parent-id'], $tax_name);
112
  $parent_id = (int) $_POST['hidden-parent-id'];
113
- if ( is_object($parent_term) && isset($parent_term->term_order) ) {
114
- $parent_id_order = (int) $parent_term->term_order;
115
- }
 
 
116
  }
117
  if (isset($_POST['return-sub-posts'])) {
118
  $parent_term = get_term($_POST['hidden-parent-id'], $tax_name);
@@ -270,7 +271,7 @@ function customtaxorder_update_order() {
270
 
271
  $parent_id_order = 0;
272
  if ( isset($_POST['hidden-parent-id-order']) && $_POST['hidden-parent-id-order'] > 0 ) {
273
- $parent_id_order = (int) $_POST['hidden-parent-id-order'] + 1;
274
  }
275
  $new_order = $_POST['hidden-custom-order'];
276
  $submitted_ids = explode(',', $new_order);
10
 
11
  $options = customtaxorder_get_settings();
12
  $taxonomies = customtaxorder_get_taxonomies();
 
13
  $this_page = sanitize_text_field( $_GET['page'] );
14
 
15
  // Set your custom capability through this filter.
103
  }
104
  }
105
 
106
+ $parent_id = 0;
107
  $parent_id_order = 0;
108
  if (isset($_POST['go-sub-posts'])) {
109
  $parent_id = (int) $_POST['sub-posts'];
110
  } else if (isset($_POST['hidden-parent-id'])) {
 
111
  $parent_id = (int) $_POST['hidden-parent-id'];
112
+ }
113
+ $parent_term = get_term( $parent_id, $tax_name );
114
+
115
+ if ( is_object($parent_term) && isset($parent_term->term_order) ) {
116
+ $parent_id_order = (int) $parent_term->term_order;
117
  }
118
  if (isset($_POST['return-sub-posts'])) {
119
  $parent_term = get_term($_POST['hidden-parent-id'], $tax_name);
271
 
272
  $parent_id_order = 0;
273
  if ( isset($_POST['hidden-parent-id-order']) && $_POST['hidden-parent-id-order'] > 0 ) {
274
+ $parent_id_order = (int) $_POST['hidden-parent-id-order'] + 1; // Not sure if we really need this parent_id_order to be added since we use the funny floats of ancestor.childchildchild.
275
  }
276
  $new_order = $_POST['hidden-custom-order'];
277
  $submitted_ids = explode(',', $new_order);
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: mpol
3
  Tags: term order, category order, taxonomy order, order
4
  Requires at least: 3.7
5
  Tested up to: 5.9
6
- Stable tag: 3.3.2
7
  License: GPLv2 or later
8
 
9
 
@@ -121,20 +121,20 @@ You can use a function to sort taxonomies themselves like this:
121
 
122
  The function requires a parameter with an array of taxonomy objects.
123
 
124
- = Is there an API? =
125
-
126
- There are actions that you can use with add_action.
127
 
128
  'customtaxorder_update_order' is being run when saving the order of terms in the admin page.
129
  You could add the following example to your functions.php and work from there.
130
 
131
  <?php
132
- function custom_action( $new_order ) {
133
  print_r( $new_order );
134
  }
135
- add_action('customtaxorder_update_order', 'custom_action');
136
  ?>
137
 
 
 
138
  'customtaxorder_terms_ordered' action is being run after term array has been ordered with usort.
139
  Please be aware that this can be triggered multiple times during a request.
140
  You could add the following example to your functions.php and work from there.
@@ -146,12 +146,45 @@ You could add the following example to your functions.php and work from there.
146
  add_action('customtaxorder_terms_ordered', 'custom_action', 10, 2);
147
  ?>
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  = I use the GET parameter 'orderby' to order posts, but then it is ignoring term order =
150
 
151
- In case the GET parameter 'orderby' is set it is assumed it is used to order terms, instead of posts, users, or anything else.
 
152
 
153
- You can add a filter to possible ignore the orderby parameter in the case that is set in the GET request.
154
- That might be usefull if your GET parameter for orderby is used to sort posts, users, or just anything that is not terms.
155
  Example code for using the filter:
156
 
157
  <?php
@@ -179,6 +212,12 @@ The left metabox lists the toplevel terms. Right (or below) are the sub-terms.
179
 
180
  == Changelog ==
181
 
 
 
 
 
 
 
182
  = 3.3.2 =
183
  * 2021-12-24
184
  * Add function 'customtaxorder_is_get_orderby_set'.
3
  Tags: term order, category order, taxonomy order, order
4
  Requires at least: 3.7
5
  Tested up to: 5.9
6
+ Stable tag: 3.3.3
7
  License: GPLv2 or later
8
 
9
 
121
 
122
  The function requires a parameter with an array of taxonomy objects.
123
 
124
+ = Can I hook into saving the term order on the dashboard page? =
 
 
125
 
126
  'customtaxorder_update_order' is being run when saving the order of terms in the admin page.
127
  You could add the following example to your functions.php and work from there.
128
 
129
  <?php
130
+ function my_customtaxorder_update_order( $new_order ) {
131
  print_r( $new_order );
132
  }
133
+ add_action('customtaxorder_update_order', 'my_customtaxorder_update_order');
134
  ?>
135
 
136
+ = Can I hook into changing the term order on the frontend? =
137
+
138
  'customtaxorder_terms_ordered' action is being run after term array has been ordered with usort.
139
  Please be aware that this can be triggered multiple times during a request.
140
  You could add the following example to your functions.php and work from there.
146
  add_action('customtaxorder_terms_ordered', 'custom_action', 10, 2);
147
  ?>
148
 
149
+ = Can I hook into the settings for each taxonomy to change the sorting behaviour? =
150
+
151
+ 'customtaxorder_settings' filter is being after fetching the settings for all taxonomies.
152
+ You could add the following example to your functions.php and work from there.
153
+
154
+ <?php
155
+ /*
156
+ * Example code to change the product_cat settings in WooCommerce on the main shop page.
157
+ *
158
+ * Settings:
159
+ * 0: orderby Term ID
160
+ * 1: orderby Custom Order
161
+ * 2: orderby Term Name (alphabetical)
162
+ * 3: orderby Term Slug (alphabetical)
163
+ *
164
+ * Taken from:
165
+ * https://wordpress.org/support/topic/product-categories-sort-how-to-sort-just-the-sub-terms-sub-categories/
166
+ *
167
+ */
168
+ function my_customtaxorder_settings( $settings ) {
169
+ //var_dump( $settings['product_cat'] );
170
+ if ( function_exists( 'is_shop' ) ) {
171
+ if ( is_shop() ) {
172
+ $settings['product_cat'] = 1;
173
+ }
174
+ }
175
+ //var_dump( $settings['product_cat'] );
176
+ return $settings;
177
+ }
178
+ add_filter( 'customtaxorder_settings', 'my_customtaxorder_settings' );
179
+ ?>
180
+
181
  = I use the GET parameter 'orderby' to order posts, but then it is ignoring term order =
182
 
183
+ In case the GET parameter 'orderby' is set it will be used to order terms, instead of posts, users, or anything else.
184
+ Therefore when that GET parameter is set, there is no custom order applied in this plugin.
185
 
186
+ You can add a filter to possible ignore the orderby parameter in the GET request.
187
+ That might be useful if your GET parameter for orderby is used to sort posts, users, or just anything that is not terms.
188
  Example code for using the filter:
189
 
190
  <?php
212
 
213
  == Changelog ==
214
 
215
+ = 3.3.3 =
216
+ * 2022-01-24
217
+ * Traverse all parents for sorting child terms, not just the toplevel ancestor.
218
+ * Some fixes on admin page for sorting subterms.
219
+ * Add filter 'customtaxorder_settings'.
220
+
221
  = 3.3.2 =
222
  * 2021-12-24
223
  * Add function 'customtaxorder_is_get_orderby_set'.