Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been struggling with this for two days. I've added an input(checkbox) to represent "Featured" categories on the 'Create a category' and 'Edit a category' pages. My problem is that when I update or create my category, the status of the checkbox(i.e. 'checked') doesn't persist. I have two other fields that I've added(one for numbers, one for text) and both of those work perfectly. To save the checkbox state, I'm using the same code that I use to save the number and text fields to my database. I think that's where I'm having a problem, as I'm not sure if I'm grabbing the right field from the checkbox. I'm pretty new to all of this, first website. Hopefully one of you more knowledgeable folks can lend me a hand, here's my code:

Adding the checkbox to the 'create a category' page:

// Adding 'Featured' checkbox to CREATE category page

function taxonomy_add_new_featured_checkbox() {
?>
<div class="form-field">
    <label for="term_meta[featured_checkbox]">Featured</label>
    <input type="checkbox" name="term_meta[featured_checkbox]" id="term_meta[featured_checkbox]">
    <p class="description">Check this box to mark this category as a "Featured" category</p>
</div>
<?php
}
add_action( 'category_add_form_fields', 'taxonomy_add_new_featured_checkbox', 10, 3 );

Adding the checkbox to the 'edit a category' page:

// Adding 'Featured' checkbox to EDIT category page
function taxonomy_edit_featured_checkbox($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[featured_checkbox]">Featured</label></th>
    <td>
        <input type="checkbox" name="term_meta[featured_checkbox]" id="term_meta[featured_checkbox]" <?php checked( $term_meta['featured_checkbox'], 1); ?> />
        <p class="description">Check this box to mark this category as a "Featured" category</p>
    </td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'taxonomy_edit_featured_checkbox', 10, 3);

The code to save these changes:

// Save extra taxonomy fields callback function
function 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] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array
        update_option( "taxonomy_$t_id", $term_meta );
    }
}
add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 3 );
add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 3 );

Thanks for reading!

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.