0

I am trying to add a simple checkbox to the category page but it not getting saved for some reason. Please help to fix the code below. Basically, I registered a meta then created check box. CheckBox is showing but the data is not getting saved

class ENWORKAdmin {

    public function __construct() {
        add_action('init', array($this, 'en_work_register_term_meta_expose'));
        add_action('category_add_form_fields', array( $this , 'en_work_add_form_fields_meta_expose') );
        add_action('category_edit_form_fields', array( $this , 'en_work_edit_form_fields_meta_expose') );
        add_action( 'edit_category', array( $this, 'en_work_save_meta_expose' ) );
        add_action( 'create_category', array( $this, 'en_work_save_meta_expose' ) );
    }


    public function en_work_register_term_meta_expose(){
        $args = array(
            'type'      => 'string',
            'single'        => true,
            'description'    => 'A meta key associated with a string meta value.',
            // 'sanitize_callback'  => array($this, 'en_work_sanitize_checkbox'),
        );
        register_meta('term', 'term_meta_expose', $args );
    }


    public function en_work_add_form_fields_meta_expose(){
        ?>

        <?php wp_nonce_field( basename( __FILE__ ), 'woocheckboxform_nonce' ); ?>
    <div class="form-field term-meta-text-wrap">
        <label for="term-meta-text"><?php _e( 'Show in Woo List', 'en_work' ); ?></label>
        <input type="checkbox" name="woocheckboxform" id="en_work-checkbox" value="" class="en_work-checkbox" />
    </div>
        <?php
    }

    public function en_work_edit_form_fields_meta_expose($term){

        $value = get_term_meta( $term->term_id, 'term_meta_expose', true );
        error_log( var_export( $value, 1 ) );

        if(!$value){
            $value = 0;
        }
        ?>
        <tr class="form-field term-meta-text-wrap">
                <th scope="row"><label for="term-meta-text"><?php _e( 'Show in Woo List', 'en_work' ); ?></label></th>
                <td>
                    <?php wp_nonce_field( basename( __FILE__ ), 'woocheckboxform_nonce' ); ?>
                                <input type="hidden" value="0" name="woocheckboxform">
                    <input type="checkbox" name="woocheckboxform" id="en_work-checkbox" value="<?php echo esc_attr( $value ); ?>" class="en_work-checkbox"  />
                </td>
         </tr>

        <?php
    }

    public function en_work_save_meta_expose($term_id){
        if ( ! isset( $_POST['woocheckboxform_nonce'] ) || ! wp_verify_nonce( $_POST['woocheckboxform_nonce'], basename( __FILE__ ) ) )
        return;

        $old_value = get_term_meta( $term_id, 'term_meta_expose', true );
        error_log( var_export( $old_value, 1 ) );

        $new_value = isset( $_POST['woocheckboxform'] ) ? $_POST['woocheckboxform'] : '';

        var_dump($old_value);

        if ( $old_value && '' === $new_value )
        delete_term_meta( $term_id, 'term_meta_expose' );

    else if ( $old_value !== $new_value )
        update_term_meta( $term_id, 'term_meta_expose', $new_value );

    }

}
6
  • What's the purpose of the check with the delete_term_meta? Should it not be update_term_meta with no checks on the values? woocheckboxform is a very generic name, you should consider something that describes what it is to avoid clashes and reuse. How are you testing that the data is not saved? Also why did you choose register_meta over register_term_meta? – Tom J Nowell Dec 26 '20 at 13:57
  • @TomJNowell I am just trying to make one work that's why I didn't much bothered about naming. I was referring to this one but I am not able to make it work for a check box : wordpress.stackexchange.com/questions/211703/… – GeekyOwl Dec 26 '20 at 14:04
  • hmm I see, but keep in mind that the hooks you've used are specific to the category taxonomy, and you haven't mentioned which taxonomy you're trying to use. We also haven't confirmed if the problem is that the data is not being saved, or if the data is being saved but the checkbox is not reflecting the saved value, especially since there is no checked/selected logic on your checkbox input – Tom J Nowell Dec 26 '20 at 21:48
  • @TomJNowell The problem is with the value parameter I had to remove it to fix the issue. I was trying to do it for a custom taxonomy - Woocommerce. The category is also a taxonomy if I am not wrong. register_meta over register_term_meta does it make any difference? I was able to make it work without a problem. – GeekyOwl Dec 27 '20 at 11:09
  • Can you post your solution as an answer? – Tom J Nowell Dec 27 '20 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.