Taxonomy custom check box meta field not getting saved

Question

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

    }

}
0
GeekyOwl 4 months 2020-12-26T08:10:18-05:00 0 Answers 4 views 0

Leave an answer

Browse
Browse