Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ordered Post Tags
=== Ordered Post Tags ===
Contributors: simonwheatley, cftp
Donate link: url
Tags: proof of concept, beta
Requires at least: 3.4.1
Tested up to: 3.4.1
This is a proof of concept plugin converting post tags into an ordered taxonomy.
== Description ==
This plugin is not recommended for production use as it overwrites a core taxonomy.
This is a proof of concept which shows how you might adapt the built-in Post Tags taxonomy so it can accept tags from the edit post UI and store/retrieve them in the specified order.
The plugin provides several template tags, they are similar enough to the core template tag equivalents without the "_ordered" suffix that you should be able to refer to the Codex for documentation; e.g. for `the_tags_ordered` refer to the documentation for `the_tags`.
<?php
/*
Plugin Name: Ordered Post Tags – Template Tags & API
Plugin URI: https://gist.github.com/3217544
Description: Proof of concept plugin, not for production use. Provides template tags and an API for those functions. Won't work without Ordered Post Tags POC plugin.
Version: 0.1
Author: Simon Wheatley @ Code for the People Ltd
Author URI: http://codeforthepeople.com
*/
/**
* Copyright 2012 Code for the People Ltd. All rights reserved
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// NOW SOME TEMPLATING FUNCTIONALITY FOR DEMO PURPOSES
// ===================================================
// N.B. Lots of duplicate code follows, were we to come to implement this in core
// it would be sensible to enhance the function equivalents of the functions
// below to accept an additional argument(s) to specify term_order.
/**
* Retrieve the terms of the taxonomy that are attached to the post
* in term_order.
*
* @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @return array|bool False on failure. Array of term objects on success.
*/
function get_the_terms_ordered( $id, $taxonomy ) {
global $post;
$id = (int) $id;
if ( !$id ) {
if ( empty( $post->ID ) )
return false;
else
$id = (int) $post->ID;
}
$terms = get_object_term_cache( $id, $taxonomy );
$terms = wp_cache_get($id, $taxonomy . '_ordered_relationships');
if ( false === $terms ) {
$terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
wp_cache_add($id, $terms, $taxonomy . '_ordered_relationships');
}
$terms = apply_filters( 'get_the_terms_ordered', $terms, $id, $taxonomy );
if ( empty( $terms ) )
return false;
return $terms;
}
/**
* Retrieve a post's terms as a list with specified format ordered by term_order.
*
* @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @return string
*/
function get_the_term_list_ordered( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms_ordered( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
/**
* Retrieve the tags for a post formatted as a string ordered by term_order.
*
* @uses apply_filters() Calls 'the_tags' filter on string list of tags.
*
* @param string $before Optional. Before tags.
* @param string $sep Optional. Between tags.
* @param string $after Optional. After tags.
* @param int $id Optional. Post ID. Defaults to the current post.
* @return string
*/
function get_the_tag_list_ordered( $before = '', $sep = '', $after = '', $id = 0 ) {
return apply_filters( 'the_tags', get_the_term_list_ordered( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
}
/**
* Print the tags for a post in term_order.
*
*
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @return string
*/
function the_tags_ordered( $before = null, $sep = ', ', $after = '' ) {
if ( null === $before )
$before = __('Tags: ');
echo get_the_tag_list_ordered($before, $sep, $after);
}
?>
<?php
/*
Plugin Name: Ordered Post Tags
Plugin URI: https://gist.github.com/3217544
Description: Proof of concept plugin, not for production use. Converts post tags to an ordered taxonomy.
Version: 0.1
Author: Simon Wheatley @ Code for the People Ltd
Author URI: http://codeforthepeople.com
*/
/**
* Copyright 2012 Code for the People Ltd. All rights reserved
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
/**
* Hooks the WordPress init action to interfere with the
* post_tag taxonomy.
*
* @return void
**/
function opt_init() {
// Get the current args for the post_tag taxonomy
$post_tag_tax = get_taxonomy( 'post_tag', ARRAY_A );
// Convert to an array
$post_tag_args = opt_convert_to_array( $post_tag_tax );
// Now make it sortable
$post_tag_args[ 'sort' ] = true;
// Exploit a feature whereby you can overwrite a taxonomy :)
register_taxonomy( 'post_tag', 'post', $post_tag_args );
}
add_action( 'init', 'opt_init' );
/**
* A utility function that recursively traverses an object
* and turns it into an associative array.
*
* @param mixed $thing Might be an object, might not be
* @return array A (possibly multidimensional) array if an object (or array) was passed in
**/
function opt_convert_to_array( & $thing ) {
$thing = get_object_vars( $thing );
if ( is_array( $thing ) )
foreach ( $thing as $key => & $_thing )
if ( is_object( $_thing ) )
$thing[ $key ] = opt_convert_to_array( $_thing );
return $thing;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.