web-dev-qa-db-fra.com

Besoin d'aide pour ajouter un champ personnalisé à la catégorie

J'essaie d'ajouter un champ personnalisé pour les catégories. Le champ personnalisé est une case à cocher unique. Je peux obtenir la case à cocher pour qu'elle apparaisse sur les formulaires de la page Créer une nouvelle catégorie et Modifier des pages de catégorie, mais si je coche la case, elle ne reste pas cochée après l'enregistrement du formulaire.

C'est le code que j'utilise:

/*  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 );

J'ai copié ce code à partir d'un tutoriel que j'ai trouvé. Le code d'origine était pour un champ personnalisé de type champ de texte, donc je pense que le problème a probablement à voir avec les paramètres de case à cocher.

3
jrcollins

Les cases à cocher sont un peu différentes des entrées de texte. Les principales modifications ci-dessous concernent la fonction de sauvegarde et la gestion de l'attribut checked. La valeur de show_category sera yes si elle a été cochée ou une chaîne vide pour non cochée.

Gardez à l'esprit que si la méta show_category n'a jamais été sauvegardée, elle ne sera pas définie, tenez-en compte dans votre 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 );
2
Dave Romsey