dimanche 24 juillet 2016

How to save checkbox value in wordpress term meta table

I create a custom post type and its taxonomy. Then I create a input[text] term meta field and save the value, it saved in wp_termmeta successfully. Here s the code.

<?php
// Add meta Field
    add_action('courses_category_add_form_fields', 'sc_add_term_values', 10, 2);
    function sc_add_term_values($taxonomy) {
?>
        <div class="form-field term-group">
            <label for="sc_txt_term_color"><?php echo 'Term Color'; ?></label>
            <input type="text" name="sc_txt_term_color" value="" class="cp-field">
        </div>
        <?php
    }

    //Save new meta value
    add_action('created_courses_category', 'save_term_values, 10, 2');
    function save_term_values($term_id, $tt_id) {

        if (isset($_POST['sc_txt_term_color']) && '' !== $_POST['sc_txt_term_color']) {
            $group = '#' . sanitize_title($_POST['sc_txt_term_color']);
            add_term_meta($term_id, 'sc_term_color', $group, true);
        }

    }

    // Get term value
    add_action('courses_category_edit_form_fields', 'edit_term_values', 10, 2);
    function edit_term_values($term, $taxonomy) {

        // get term value
        $sc_txt_term_color = get_term_meta($term->term_id, 'sc_term_color', true);
?>
        <tr class="form-field term-group-wrap">
            <th scope="row">
                <label for="sc_txt_term_color"><?php echo 'Term Color'; ?></label>
            </th>
            <td>
                <input type="text" name="sc_txt_term_color" value="<?php echo $sc_txt_term_color ?>" class="cp-field">
            </td>
        </tr>
<?php
    }

    // Update term value
    add_action('edited_category', 'update_term_values', 10, 2);
    function update_term_values($term_id, $tt_id) {
        if (isset($_POST['wm_txt_term_color']) && '' !== $_POST['wm_txt_term_color']) {
            $group = '#' . sanitize_title($_POST['wm_txt_term_color']);
            update_term_meta($term_id, 'wm_term_color', $group);
        }
    }
?>

But When i create a checkbox term meta field and saved its value, the value does not save in wp_termmeta

Here is my code:

<?php
// Add meta Field
    add_action('courses_category_add_form_fields', 'sc_add_term_values', 10, 2);
    function sc_add_term_values($taxonomy) {
?>
        <div class="form-field term-group">
            <label for="sc_txt_term_color"><?php echo 'Term Color'; ?></label>
            <input type="text" name="sc_txt_term_color" value="" class="cp-field">
        </div>

        <div class="form-field term-group">
            <label for="sc_chk_term_featured"><?php echo 'Term Featured'; ?></label>
            <input type="checkbox" name="sc_chk_term_featured">
        </div>
        <?php
    }

    //Save new meta value
    add_action('created_courses_category', 'save_term_values, 10, 2');
    function save_term_values($term_id, $tt_id) {

        if (isset($_POST['sc_txt_term_color']) && '' !== $_POST['sc_txt_term_color']) {
            $group = '#' . sanitize_title($_POST['sc_txt_term_color']);
            add_term_meta($term_id, 'sc_term_color', $group, true);
        }

        $sc_chk_term_featured = ($_POST['sc_chk_term_featured'] == 'on' ? '1' : '0');
        add_term_meta($term_id, 'sc_term_featured', $sc_chk_term_featured, true);

    }

    // Get term value
    add_action('courses_category_edit_form_fields', 'edit_term_values', 10, 2);
    function edit_term_values($term, $taxonomy) {

        // get term value
        $sc_txt_term_color = get_term_meta($term->term_id, 'sc_term_color', true);
?>
        <tr class="form-field term-group-wrap">
            <th scope="row">
                <label for="sc_txt_term_color"><?php echo 'Term Color'; ?></label>
            </th>
            <td>
                <input type="text" name="sc_txt_term_color" value="<?php echo $sc_txt_term_color ?>" class="cp-field">
            </td>
        </tr>
<?php
    }

    // Update term value
    add_action('edited_category', 'update_term_values', 10, 2);
    function update_term_values($term_id, $tt_id) {
        if (isset($_POST['wm_txt_term_color']) && '' !== $_POST['wm_txt_term_color']) {
            $group = '#' . sanitize_title($_POST['wm_txt_term_color']);
            update_term_meta($term_id, 'wm_term_color', $group);
        }
    }
?>

So please guide me that how can I save checkbox or radio button value in wp_termmeta




Aucun commentaire:

Enregistrer un commentaire