· 9 years ago · Dec 23, 2016, 12:30 PM
1function hm_add_term_meta_table() {
2 global $wpdb;
3 if ( ! current_theme_supports( 'term-meta' ) )
4 return false;
5 hm_create_term_meta_table();
6 $wpdb->tables[] = 'termmeta';
7 $wpdb->termmeta = $wpdb->prefix . 'termmeta';
8}
9add_action( 'init', 'hm_add_term_meta_table' );
10/**
11 * Create the termmeta table if it doesn't exist
12 *
13 * @todo should check if the table exists directly rather than relying on an option
14 */
15function hm_create_term_meta_table() {
16
17 global $wpdb;
18 // check if the table already exists
19 if ( get_option( 'hm_created_term_meta_table' ) )
20 return false;
21 $wpdb->query( "
22 CREATE TABLE `{$wpdb->prefix}termmeta` (
23 `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
24 `term_id` bigint(20) unsigned NOT NULL DEFAULT '0',
25 `meta_key` varchar(255) DEFAULT NULL,
26 `meta_value` longtext,
27 PRIMARY KEY (`meta_id`),
28 KEY `term_id` (`term_id`),
29 KEY `meta_key` (`meta_key`)
30 ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;" );
31 update_option( 'hm_created_term_meta_table', true );
32 return true;
33}
34if ( ! function_exists( 'add_term_meta' ) ) :
35/**
36 * Add meta data field to a term.
37 *
38 * @param int $term_id term_id.
39 * @param string $key Metadata name.
40 * @param mixed $value Metadata value.
41 * @param bool $unique Optional, default is false. Whether the same key should not be added.
42 * @return bool False for failure. True for success.
43 */
44function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
45 return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
46}
47endif;
48if ( ! function_exists( 'delete_term_meta' ) ) :
49/**
50 * Remove metadata matching criteria from a term.
51 *
52 * You can match based on the key, or key and value. Removing based on key and
53 * value, will keep from removing duplicate metadata with the same key. It also
54 * allows removing all metadata matching key, if needed.
55 *
56 * @param int $term_id term_id
57 * @param string $meta_key Metadata name.
58 * @param mixed $meta_value Optional. Metadata value.
59 * @return bool False for failure. True for success.
60 */
61function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
62 return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
63}
64endif;
65if ( ! function_exists( 'get_term_meta' ) ) :
66/**
67 * Retrieve term meta field for a term.
68 *
69 * @param int $term_id term_id.
70 * @param string $key The meta key to retrieve.
71 * @param bool $single Whether to return a single value.
72 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
73 * is true.
74 */
75function get_term_meta( $term_id, $key, $single = false ) {
76 return get_metadata( 'term', $term_id, $key, $single );
77}
78endif;
79if ( ! function_exists( 'update_term_meta' ) ) :
80/**
81 * Update term meta field based on term_id.
82 *
83 * Use the $prev_value parameter to differentiate between meta fields with the
84 * same key and term_id.
85 *
86 * If the meta field for the term does not exist, it will be added.
87 *
88 * @param int $term_id term ID.
89 * @param string $key Metadata key.
90 * @param mixed $value Metadata value.
91 * @param mixed $prev_value Optional. Previous value to check before removing.
92 * @return bool False on failure, true if success.
93 */
94function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
95 return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
96}
97endif;
98if ( ! function_exists( 'get_term_custom' ) ) :
99/**
100 * Retrieve term meta fields, based on term_id.
101 *
102 * The term meta fields are retrieved from the cache, so the function is
103 * optimized to be called more than once. It also applies to the functions, that
104 * use this function.
105 *
106 * @param int $term_id term_id
107 * @return array
108 */
109 function get_term_custom( $term_id = 0 ) {
110 $term_id = (int) $term_id;
111 if ( ! wp_cache_get( $term_id, 'term_meta' ) )
112 update_termmeta_cache( $term_id );
113 return wp_cache_get( $term_id, 'term_meta' );
114}
115endif;
116if ( ! function_exists( 'update_termmeta_cache' ) ) :
117/**
118* Updates metadata cache for list of term_ids.
119*
120* Performs SQL query to retrieve the metadata for the term_ids and updates the
121* metadata cache for the terms. Therefore, the functions which call this
122* function do not need to perform SQL queries on their own.
123*
124* @param array $term_ids List of term_ids.
125* @return bool|array Returns false if there is nothing to update or an array of metadata.
126*/
127function update_termmeta_cache( $term_ids ) {
128 return update_meta_cache( 'term', $term_ids );
129}
130endif;
131add_theme_support( 'term-meta' );