Created
May 18, 2012 20:05
-
-
Save alexkingorg/2727339 to your computer and use it in GitHub Desktop.
WordPress Post Formats as Categories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Formats as Categories | |
| Plugin URI: http://alexking.org/projects/wordpress | |
| Description: Have enabled post formats show up as categories for selection via XML-RPC clients (iPhone, Android, etc.). | |
| Version: 1.0dev | |
| Author: Crowd Favorite | |
| Author URI: http://crowdfavorite.com | |
| */ | |
| // ini_set('display_errors', '1'); ini_set('error_reporting', E_ALL); | |
| load_plugin_textdomain('formats-as-cats'); | |
| /* TODO | |
| - test on XMLRPC | |
| */ | |
| define('FASC_CAT_OFFSET', 1000000); | |
| function fasc_create_term($format, $cat_id) { | |
| $term = new stdClass; | |
| $term->term_id = $cat_id; | |
| $term->name = sprintf(__('Format: %s', 'formats-as-cats'), ucwords($format)); | |
| $term->slug = 'fasc-format-'.$format; | |
| $term->term_group = 0; | |
| $term->term_taxonomy_id = 1; | |
| $term->taxonomy = 'category'; | |
| $term->description = ''; | |
| $term->parent = 0; | |
| $term->count = 0; | |
| return $term; | |
| } | |
| function fasc_get_terms($terms, $taxonomies) { | |
| // if (!defined('XMLRPC_REQUEST') || !current_theme_supports('post-formats') || !in_array('category', $taxonomies)) { | |
| // return $terms; | |
| // } | |
| $formats = get_theme_support('post-formats'); | |
| if (is_array($formats)) { | |
| $formats = $formats[0]; | |
| $i = 0; | |
| $format_terms = array(); | |
| foreach ($formats as $format) { | |
| $format_terms[] = fasc_create_term($format, FASC_CAT_OFFSET + $i); | |
| $i++; | |
| } | |
| $terms = array_merge($format_terms, $terms); | |
| } | |
| return $terms; | |
| } | |
| add_filter('get_terms', 'fasc_get_terms', 10, 2); | |
| function fasc_get_the_terms($terms, $id, $taxonomy) { | |
| // if (!defined('XMLRPC_REQUEST') || !current_theme_supports('post-formats') || $taxonomy != 'category') { | |
| // return $terms; | |
| // } | |
| remove_filter('get_the_terms', 'fasc_get_the_terms', 10, 3); | |
| if ($format = get_post_format($id)) { | |
| $terms[] = fasc_create_term($format, fasc_format_to_cat_id($format)); | |
| } | |
| add_filter('get_the_terms', 'fasc_get_the_terms', 10, 3); | |
| return $terms; | |
| } | |
| add_filter('get_the_terms', 'fasc_get_the_terms', 10, 3); | |
| function fasc_init() { | |
| // if (!defined('XMLRPC_REQUEST') || !current_theme_supports('post-formats')) { | |
| // return; | |
| // } | |
| // This works for the web, but we probably need to convert it run something similar on save for XMLRPC | |
| if (isset($_POST['post_category']) && is_array($_POST['post_category'])) { | |
| $cats = array(); | |
| foreach ($_POST['post_category'] as $cat_id) { | |
| if ($cat_id < FASC_CAT_OFFSET) { | |
| $cats[] = $cat_id; | |
| } | |
| else { | |
| $format = fasc_cat_id_to_format($cat_id); | |
| if ($format) { | |
| $_POST['post_format'] = $format; | |
| } | |
| } | |
| } | |
| $_POST['post_category'] = $cats; | |
| } | |
| } | |
| add_action('admin_init', 'fasc_init'); | |
| function fasc_cat_id_to_format($cat_id) { | |
| if (!current_theme_supports('post-formats')) { | |
| return false; | |
| } | |
| $formats = get_theme_support('post-formats'); | |
| if (is_array($formats)) { | |
| $formats = $formats[0]; | |
| $i = 0; | |
| $format_terms = array(); | |
| foreach ($formats as $format) { | |
| $test = FASC_CAT_OFFSET + $i; | |
| if ($test == $cat_id) { | |
| return $format; | |
| } | |
| $i++; | |
| } | |
| } | |
| return false; | |
| } | |
| function fasc_format_to_cat_id($format_str) { | |
| if (!current_theme_supports('post-formats')) { | |
| return false; | |
| } | |
| $formats = get_theme_support('post-formats'); | |
| if (is_array($formats)) { | |
| $formats = $formats[0]; | |
| $i = 0; | |
| $format_terms = array(); | |
| foreach ($formats as $format) { | |
| if ($format_str == $format) { | |
| return FASC_CAT_OFFSET + $i; | |
| } | |
| $i++; | |
| } | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment