vendredi 22 mai 2020

saving checkbox values in meta box and displayed on front-end (category page not post/page) in wordpress

I have create multiple checkbox in meta box using this code

add_action ( 'edit_category_form_fields', 'extra_category_fields');

function extra_category_fields( $tag ) {
    $t_id = $tag->term_id;
    $cat_meta = get_option( "category_$t_id");
    ?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="extra1"><?php _e('extra field'); ?></label></th>
    <td>
        <input type="text" name="Cat_meta[extra1]" id="Cat_meta[extra1]" size="25" style="width:60%;" value="<?php echo $cat_meta['extra1'] ? $cat_meta['extra1'] : ''; ?>"><br />
        <span class="description"><?php _e('extra field'); ?></span>
    </td>
</tr>

<tr class="form-field">
    <th scope="row" valign="top"><label><?php _e('extra field'); ?></label></th>
    <td>
        <input type="radio" name="radio" value="ongoing"  <?php checked( 'ongoing', get_option('radio') ); ?> >
        <label class="description" for="ongoing">ongoing</label><br>
        <input type="radio" name="radio" value="complated"  <?php checked( 'complated', get_option('radio') ); ?> >
        <label class="description" for="complated">complated</label>
    </td>
</tr>

<tr class="form-field">
    <th scope="row" valign="top"><label><?php _e('extra field'); ?></label></th>
    <td>
        <label class="checkbox-inline" for="1">
            <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 1</label>
        <label class="checkbox-inline" for="2">
            <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 2</label>
        <label class="checkbox-inline" for="3">
            <input type="checkbox" name="checkbox"<?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 3</label>
    </td>
</tr>

<?php
}
add_action ( 'edit_category_form_fields', 'extra_category_fields');

for saving:

function save_extra_category_fileds( $term_id) {
    if ( isset( $_POST['Cat_meta'] ) ) {
        $t_id = $term_id;
        $cat_meta = get_option( "category_$t_id");
        $cat_keys = array_keys($_POST['Cat_meta']);

        foreach ($cat_keys as $key){
            if (isset($_POST['Cat_meta'][$key])){
                $cat_meta[$key] = $_POST['Cat_meta'][$key];
            }
            if (isset($_POST['radio'])) {
                update_option('radio', $_POST['radio']);
            }
            if (isset($_POST['checkbox'])); {
                update_option('checkbox', $_POST['checkbox']); 
            }
        }

        update_option( "category_$t_id", $cat_meta );
    }
}
add_action ( 'edited_category', 'save_extra_category_fileds');

radio option working fine but when I check one of checkbox and refresh the page it goes back to unchecked. Category fields not same normal pages so I cant use update_post_meta or $post_id

so plz help me in this ... thank you




Aucun commentaire:

Enregistrer un commentaire