Skip to content

Instantly share code, notes, and snippets.

@rezzz-dev
Created August 27, 2012 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezzz-dev/3489914 to your computer and use it in GitHub Desktop.
Save rezzz-dev/3489914 to your computer and use it in GitHub Desktop.
Adding a checkbox to meta fields
// Add term page
function summit_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[displayed]"><?php _e( 'Display Category', 'summit' ); ?></label>
<input type="hidden" name="term_meta[helpme]" value="z">
<input type="checkbox" name="term_meta[displayed]" id="term_meta[displayed]" value="1">
<p class="description"><?php _e( 'Check to have the Category to displayed on the Sales Page','summit' ); ?></p>
</div>
<?php
}
add_action( 'category_add_form_fields', 'summit_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function summit_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" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[displayed]"><?php _e( 'Display Category', 'summit' ); ?></label></th>
<td>
<input type="hidden" name="term_meta[helpme]" value="z">
<input type="checkbox" name="term_meta[displayed]" id="term_meta[displayed]" value="1" <?php if( isset( $term_meta['displayed'] ) ) { ?>checked="checked"<?php } ?> ">
<p class="description"><?php _e( 'Check to have the Category to displayed on the Sales Page','summit' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'summit_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
$new_meta = array();
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$new_meta[$key] = $_POST['term_meta'][$key];
}
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $new_meta );
}
add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment