Добавить поле в категории Woocommerce. Custom field Woocommerce


Добавление дополнительного поля в категории плагина woocommerce. Поле с визуальным редактором от WordPress.

<?php
add_action( 'product_cat_add_form_fields', 'taxonomy_add_meta_field', 10, 2 );
// A callback function to add a custom field to our "presenters" taxonomy
function taxonomy_add_meta_field($tag) {
    // Check for existing taxonomy meta for the term you're editing
    ?>
    <div class="form-field">
        <label for="term_meta[description_bottom]"><?php _e( 'Description after Products', 'project_wpml' ); ?></label>
        <textarea type="text" class="wp-editor-area" name="term_meta[description_bottom]" id="term_meta[description_bottom]" value=""></textarea>
        <p class="description"><?php _e( 'Enter a value for this field','project_wpml' ); ?></p>
    </div>
    <?php
}

add_action( 'product_cat_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2 );
// Edit term page
function taxonomy_edit_meta_field($term) {
    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" );
    $settings = array('textarea_name' =>  "term_meta[description_bottom]" );
    ?>

    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[description_bottom]"><?php _e( 'Description after Products', 'project_wpml' ); ?></label></th>
        <td>
            <?php wp_editor( $term_meta['description_bottom'],  'description_bottom', $settings ); ?>
            <!--<textarea name="term_meta[description_bottom]" id="term_meta[description_bottom]" value="<?php echo esc_attr( $term_meta['description_bottom'] ) ? esc_attr( $term_meta['description_bottom'] ) : ''; ?>"></textarea>-->
            <p class="description"><?php _e( 'Enter a value for this field','project_wpml' ); ?></p>
        </td>
    </tr>
    <?php
}

function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
?>