WordPress.org

Make WordPress Core

Ticket #9547: ordered-post-tags.php

File ordered-post-tags.php, 2.2 KB (added by simonwheatley, 3 years ago)

Non-production plugin to turn post_tag into a sorted taxonomy

Line 
1<?php
2
3/*
4Plugin Name: Ordered Post Tags
5Plugin URI: https://gist.github.com/3217544
6Description: Proof of concept plugin, not for production use. Converts post tags to an ordered taxonomy.
7Version: 0.1
8Author: Simon Wheatley @ Code for the People Ltd
9Author URI: http://codeforthepeople.com
10*/
11
12/**
13 * Copyright 2012 Code for the People Ltd. All rights reserved
14 * Released under the GPL license
15 * http://www.opensource.org/licenses/gpl-license.php
16 *
17 * This is an add-on for WordPress
18 * http://wordpress.org/
19 *
20 * **********************************************************************
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 * **********************************************************************
31 */
32
33/**
34 * Hooks the WordPress init action to interfere with the
35  * post_tag taxonomy.
36 *
37 * @return void
38 **/
39function opt_init() {
40        // Get the current args for the post_tag taxonomy
41        $post_tag_tax = get_taxonomy( 'post_tag', ARRAY_A );
42        // Convert to an array
43        $post_tag_args = opt_convert_to_array( $post_tag_tax );
44        // Now make it sortable
45        $post_tag_args[ 'sort' ] = true;
46        // Exploit a feature whereby you can overwrite a taxonomy :)
47        register_taxonomy( 'post_tag', 'post', $post_tag_args );
48}
49add_action( 'init', 'opt_init' );
50
51/**
52 * A utility function that recursively traverses an object
53 * and turns it into an associative array.
54 *
55 * @param mixed $thing Might be an object, might not be
56 * @return array A (possibly multidimensional) array if an object (or array) was passed in
57 **/
58function opt_convert_to_array( & $thing ) {
59        $thing = get_object_vars( $thing );
60        if ( is_array( $thing ) )
61                foreach ( $thing as $key => & $_thing )
62                        if ( is_object( $_thing ) )
63                                $thing[ $key ] = opt_convert_to_array( $_thing );
64        return $thing;
65}
66
67?>