Last active
August 29, 2015 14:16
-
-
Save ScreamingDev/688e07881d4d3b853482 to your computer and use it in GitHub Desktop.
Meta-Field for Taxonomy / Terms
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 | |
// for activation hook ;) | |
/** @var \wpdb $wpdb */ | |
global $wpdb; | |
$type = 'forum_term'; | |
$table_name = $wpdb->prefix . $type . 'meta'; | |
$charset_collate = ''; | |
if ( ! empty ( $wpdb->charset ) ) { | |
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; | |
} | |
if ( ! empty ( $wpdb->collate ) ) { | |
$charset_collate .= " COLLATE {$wpdb->collate}"; | |
} | |
$sql = "CREATE TABLE IF NOT EXISTS {$table_name} ( | |
meta_id bigint(20) NOT NULL AUTO_INCREMENT, | |
{$type}_id bigint(20) NOT NULL default 0, | |
meta_key varchar(255) DEFAULT NULL, | |
meta_value longtext DEFAULT NULL, | |
UNIQUE KEY meta_id (meta_id) | |
) {$charset_collate};"; | |
$wpdb->query( $sql ); |
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 | |
define( 'FORUM_TERM', 'forum_term' ); | |
/** @var \wpdb $wpdb */ | |
global $wpdb; | |
$identifier = FORUM_TERM . 'meta'; | |
$wpdb->$identifier = $wpdb->prefix . $identifier; | |
add_action( 'edit_tag_form_fields', 'forum_termmeta_tag_input_metabox' ); | |
add_action( 'edited_terms', 'forum_termmeta_save_tag_data' ); | |
function forum_termmeta_tag_input_metabox( $tag ) { | |
$forum_short = get_metadata( | |
FORUM_TERM, | |
$tag->term_id, | |
'forum_short', | |
true | |
); | |
$forum_sort = get_metadata( | |
FORUM_TERM, | |
$tag->term_id, | |
'forum_sort', | |
true | |
); | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label | |
for="tag_widget"><?php _e( 'Short' ) ?></label></th> | |
<td> | |
<input type="text" name="forum_short" id="forum_short" | |
value="<?php echo $forum_short ?>"/> | |
</td> | |
</tr> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label | |
for="tag_widget"><?php _e( 'Sort' ) ?></label></th> | |
<td> | |
<input type="text" name="forum_sort" id="forum_sort" | |
value="<?php echo $forum_sort ?>"/> | |
</td> | |
</tr> | |
<?php | |
} | |
function forum_termmeta_save_tag_data( $term_id ) { | |
if ( isset( $_POST['forum_short'] ) ) { | |
$tag_metadata = esc_attr( $_POST['forum_short'] ); | |
update_metadata( | |
FORUM_TERM, | |
$term_id, | |
'forum_short', | |
$tag_metadata | |
); | |
} | |
if ( isset( $_POST['forum_sort'] ) ) { | |
$tag_metadata = esc_attr( $_POST['forum_sort'] ); | |
update_metadata( | |
FORUM_TERM, | |
$term_id, | |
'forum_sort', | |
$tag_metadata | |
); | |
} | |
} |
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 | |
public function get_meta_result($meta_query, $type, $base_table, $id_field ) { | |
/** @var \wpdb $wpdb */ | |
global $wpdb; | |
$sql = get_meta_sql( | |
$meta_query, | |
$type, | |
$base_table, | |
$id_field | |
); | |
$join = ''; | |
if (isset($sql['join'])) { | |
$join .= $sql['join']; | |
} | |
$where = ' 1=1 '; | |
if (isset($sql['where'])) { | |
$where .= $sql['where']; | |
} | |
$result = $wpdb->get_col( | |
'SELECT * FROM ' . $base_table | |
. $join . | |
' WHERE ' . $where | |
); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment