$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Need help adding custom field to category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Need help adding custom field to category

matteradmin9PV0评论

I'm trying to add a custom field for categories. The custom field is a single checkbox. I can get the checkbox to show up on the forms in the create new category and edit category pages but if I check the checkbox it isn't staying checked after saving the form.

This is the code I'm using:

/*  Custom Field for Categories.
    ======================================== */

//Add new page

function my_taxonomy_add_meta_fields( $taxonomy ) {
    ?>
    <div class="form-field term-group">
        <label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
        <input type="checkbox" id="show_category" name="show_category" />
    </div>
    <?php
}
add_action( 'category_add_form_fields', 'my_taxonomy_add_meta_fields', 10, 2 );

//Edit term page

function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
    $show_category = get_term_meta( $term->term_id, 'show_category', true );
    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
        </th>
        <td>
            <input type="checkbox" id="show_category" name="show_category" value="<?php echo $show_category; ?>" />
        </td>
    </tr>
    <?php
}
add_action( 'category_edit_form_fields', 'my_taxonomy_edit_meta_fields', 10, 2 );

//Save custom meta

function my_taxonomy_save_taxonomy_meta( $term_id, $tag_id ) {
    if( isset( $_POST['show_category'] ) ) {
        update_term_meta( $term_id, 'show_category', esc_attr( $_POST['show_category'] ) );
    }
}
add_action( 'created_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
add_action( 'edited_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );

I copied this code from a tutorial I found. The original code was for a text field type custom field so I think the problem probably has to do with the checkbox settings.

I'm trying to add a custom field for categories. The custom field is a single checkbox. I can get the checkbox to show up on the forms in the create new category and edit category pages but if I check the checkbox it isn't staying checked after saving the form.

This is the code I'm using:

/*  Custom Field for Categories.
    ======================================== */

//Add new page

function my_taxonomy_add_meta_fields( $taxonomy ) {
    ?>
    <div class="form-field term-group">
        <label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
        <input type="checkbox" id="show_category" name="show_category" />
    </div>
    <?php
}
add_action( 'category_add_form_fields', 'my_taxonomy_add_meta_fields', 10, 2 );

//Edit term page

function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
    $show_category = get_term_meta( $term->term_id, 'show_category', true );
    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
        </th>
        <td>
            <input type="checkbox" id="show_category" name="show_category" value="<?php echo $show_category; ?>" />
        </td>
    </tr>
    <?php
}
add_action( 'category_edit_form_fields', 'my_taxonomy_edit_meta_fields', 10, 2 );

//Save custom meta

function my_taxonomy_save_taxonomy_meta( $term_id, $tag_id ) {
    if( isset( $_POST['show_category'] ) ) {
        update_term_meta( $term_id, 'show_category', esc_attr( $_POST['show_category'] ) );
    }
}
add_action( 'created_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
add_action( 'edited_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );

I copied this code from a tutorial I found. The original code was for a text field type custom field so I think the problem probably has to do with the checkbox settings.

Share Improve this question edited Sep 22, 2016 at 4:03 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Sep 9, 2016 at 1:22 jrcollinsjrcollins 5712 gold badges15 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Checkboxes are a bit different than text inputs. The main changes below are in the save function and with handling the checked attribute. The value of show_category will be yes if it's been checked or an empty string for unchecked.

Keep in mind that if the show_category meta was never saved it will be unset, so take that into account in your code.

/*  Custom Field for Categories.
    ======================================== */

// Add new term page
function my_taxonomy_add_meta_fields( $taxonomy ) { ?>
    <div class="form-field term-group">
        <label for="show_category">
          <?php _e( 'Show Category', 'codilight-lite' ); ?> <input type="checkbox" id="show_category" name="show_category" value="yes" />
        </label>
    </div><?php
}
add_action( 'category_add_form_fields', 'my_taxonomy_add_meta_fields', 10, 2 );

// Edit term page
function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
    $show_category = get_term_meta( $term->term_id, 'show_category', true ); ?>

    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
        </th>
        <td>
            <input type="checkbox" id="show_category" name="show_category" value="yes" <?php echo ( $show_category ) ? checked( $show_category, 'yes' ) : ''; ?>/>
        </td>
    </tr><?php
}
add_action( 'category_edit_form_fields', 'my_taxonomy_edit_meta_fields', 10, 2 );

// Save custom meta
function my_taxonomy_save_taxonomy_meta( $term_id, $tag_id ) {
    if ( isset( $_POST[ 'show_category' ] ) ) {
        update_term_meta( $term_id, 'show_category', 'yes' );
    } else {
        update_term_meta( $term_id, 'show_category', '' );
    }
}
add_action( 'created_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
add_action( 'edited_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
Post a comment

comment list (0)

  1. No comments so far