Last active
July 7, 2021 17:57
-
-
Save chrisdc/ed5b24398dbff60ef81e to your computer and use it in GitHub Desktop.
Add a custom meta box to the new/edit category pages. The meta data is saved to the array term_meta[], which can handle further fields in the future. Based on: https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/
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 | |
/** | |
* Add a custom meta box to the new/edit category pages. | |
* The meta data is saved to the array term_meta[], which can handle further | |
* fields in the future. | |
* | |
* Based on: https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/ | |
*/ | |
/** | |
* Add meta box to the new category page. | |
*/ | |
function chrisdc_taxonomy_add_new_meta_field() { | |
// This will add the custom meta field to the add new term page. | |
ob_start(); ?> | |
<div class="form-field"> | |
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'chrisdc' ); ?></label> | |
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value=""> | |
<p class="description"><?php _e( 'Enter a value for this field', 'chrisdc' ); ?></p> | |
<?php wp_nonce_field ( 'update_term_meta', 'term_meta_nonce' ) ?> | |
</div> | |
<?php ob_end_flush(); | |
} | |
add_action( 'category_add_form_fields', 'wctools_taxonomy_add_new_meta_field', 10, 2 ); | |
/** | |
*Add meta box to the term category page. | |
*/ | |
function chrisdc_taxonomy_edit_meta_field($term) { | |
// Put the term ID into a variable. | |
$t_id = $term->term_id; | |
// Retrieve the existing value(s) for this meta field. This returns an array. | |
$term_meta = get_option( "taxonomy_$t_id" ); | |
ob_start(); ?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'chrisdc' ); ?></label></th> | |
<td> | |
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>"> | |
<p class="description"><?php _e( 'Enter a value for this field', 'chrisdc' ); ?></p> | |
<?php wp_nonce_field ( 'update_term_meta', 'term_meta_nonce' ) ?> | |
</td> | |
</tr> | |
<?php ob_end_flush(); | |
} | |
add_action( 'category_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 ); | |
/** | |
* Save meta data callback function. | |
*/ | |
function chrisdc_save_taxonomy_custom_meta( $term_id ) { | |
if ( isset( $_POST['term_meta'] ) ) { | |
$t_id = $term_id; | |
$term_meta = get_option( "taxonomy_$t_id" ); | |
$cat_keys = array_keys( $_POST['term_meta'] ); | |
foreach ( $cat_keys as $key ) { | |
if ( isset ( $_POST['term_meta'][$key] ) ) { | |
$term_meta[$key] = sanitize_text_field ( $_POST['term_meta'][$key] ); | |
} | |
} | |
// Save the option array. | |
update_option( "taxonomy_$t_id", $term_meta ); | |
} | |
} | |
add_action( 'edited_category', 'chrisdc_save_taxonomy_custom_meta', 10, 2 ); | |
add_action( 'create_category', 'chrisdc_save_taxonomy_custom_meta', 10, 2 ); |
<?php
/**
*Add meta box to the term category page.
*/
function dtbaker_taxonomy_edit_meta_field( $term ) {
// Retrieve the existing value(s) for this meta field.
$term_meta = $term && !empty($term->term_id) ? get_term_meta( $term->term_id, 'custom_page_id', true ) : false;
?>
<tr class="form-field">
<th scope="row" valign="top"><label
for="term_meta[custom_page_id]"><?php _e( 'Choose a page', 'dtbaker' ); ?></label></th>
<td>
<?php
$dropdown_args = array(
'post_type' => 'page',
'selected' => $term_meta,
'name' => 'term_meta[custom_page_id]',
'show_option_none' => __( '(no page)' ),
'sort_column' => 'menu_order, post_title',
'echo' => 1,
);
wp_dropdown_pages( $dropdown_args );
?>
<p class="description"><?php _e( 'Choose a page', 'dtbaker' ); ?></p>
<?php wp_nonce_field( 'update_term_meta', 'term_meta_nonce' ) ?>
</td>
</tr>
<?php
}
add_action( 'category_add_form_fields', 'dtbaker_taxonomy_edit_meta_field', 10 );
add_action( 'category_edit_form_fields', 'dtbaker_taxonomy_edit_meta_field', 10 );
/**
* Save meta data callback function.
*/
function dtbaker_save_taxonomy_custom_meta( $term_id ) {
if (
isset( $_POST['term_meta'] ) && is_array( $_POST['term_meta'] ) &&
! empty( $_POST['term_meta_nonce'] ) && wp_verify_nonce( $_POST['term_meta_nonce'], 'update_term_meta' )
) {
foreach ( $_POST['term_meta'] as $key => $value ) {
update_term_meta( $term_id, $key, sanitize_text_field( $value ) );
}
}
}
add_action( 'edited_category', 'dtbaker_save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_category', 'dtbaker_save_taxonomy_custom_meta', 10, 2 );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no check against
term_meta_nonce
and this stores meta values in option table when it should useget_term_meta
andupdate_term_meta
. New code incoming...