Created
July 22, 2015 07:24
-
-
Save RadGH/a038cbe14780d05f41cd to your computer and use it in GitHub Desktop.
Magic term conversion with WordPress
This file contains 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 | |
/* | |
Takes a term from many input types: object, id, slug, name | |
Returns one of many outputs: WP_Term (object), id (int), slug, name, or any WP_Term property | |
Example Inputs: | |
smart_term_convert( 390, 'category', 'id' ); // Returns 390 | |
smart_term_convert( 'test-category', 'category', 'id' ); // Returns 390 | |
smart_term_convert( 'Test Category', 'category', 'id' ); // Returns 390 | |
smart_term_convert( $test_category_term, 'category', 'id' ); // Returns 390 | |
Example Outputs: | |
smart_term_convert( 390, 'category', 'object' ); // Returns OBJECT | |
smart_term_convert( 390, 'category', 'term_id' ); // Returns (int) 390 | |
smart_term_convert( 390, 'category', 'id' ); // Returns (int) 390, alias of term_id | |
smart_term_convert( 390, 'category', 'name' ); // Returns "Test Category" | |
smart_term_convert( 390, 'category', 'title' ); // Returns "Test Category", alias of name | |
smart_term_convert( 390, 'category', 'slug' ); // Returns "test-category" | |
smart_term_convert( 390, 'category', 'url' ); // Returns "http://..../test-category/" | |
smart_term_convert( 390, 'category', 'taxonomy' );// Returns "category", albeit pointless here | |
Test case: | |
echo '<pre>'; | |
$keys = 'term_id|id|name|title|slug|url|object|taxonomy|term_taxonomy_id|fake_term_name'; | |
foreach( explode('|', $keys) as $key ) echo $key, ': ', print_r( smart_term_convert( get_query_var( 'catalog' ), 'catalog', $key ), true ), "\n"; | |
echo '</pre>'; | |
Test case output: | |
term_id: 171 | |
id: 171 | |
name: FALL 15 | |
title: FALL 15 | |
slug: fall-15 | |
url: http://.../catalog/fall-15/ | |
object: stdClass Object | |
[term_id] => 171 | |
[name] => FALL 15 | |
[slug] => fall-15 | |
[term_group] => 0 | |
[term_taxonomy_id] => 171 | |
[taxonomy] => catalog | |
[description] => | |
[parent] => 0 | |
[count] => 8 | |
[filter] => raw | |
taxonomy: catalog | |
term_taxonomy_id: 171 | |
fake_term_name: FALSE | |
*/ | |
function smart_term_convert( $term_data, $taxonomy, $output = 'object' ) { | |
if ( !$term_data ) return false; | |
$term = false; | |
// input: object | |
if ( !$term && is_object( $term_data ) ) { | |
$term = $term_data; | |
} | |
// input: slug, title | |
if ( !$term && is_string( $term_data ) ) { | |
$term = get_term_by( 'slug', $term_data, $taxonomy ); | |
if ( !$term ) $term = get_term_by( 'name', $term_data, $taxonomy ); | |
} | |
// input: term_id | |
if ( !$term && is_numeric( $term_data ) ) { | |
$term = get_term_by( 'id', (int) $term_data, $taxonomy ); | |
} | |
// No object found! | |
if ( !$term ) return false; | |
switch( strtolower($output) ) { | |
case 'object': | |
return $term; | |
break; | |
case 'name': | |
case 'title': | |
return $term->name; | |
break; | |
case 'term_id': | |
case 'id': | |
return $term->term_id; | |
break; | |
case 'slug': | |
return $term->slug; | |
break; | |
case 'url': | |
return get_term_link($term); | |
break; | |
default: | |
if ( !$output ) return $term; | |
if ( !property_exists($term, $output) ) return false; | |
// other properties | |
return $term->{$output}; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment