Created
August 5, 2015 10:21
-
-
Save dededey/36021b7ba51560e2e95e to your computer and use it in GitHub Desktop.
add custom field to woocommerce product category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add term page | |
function custom_url_taxonomy_add_new_meta_field() { | |
// this will add the custom meta field to the add new term page | |
?> | |
<div class="form-field"> | |
<label for="term_meta[custom_term_meta]"><?php _e( 'Custom url category', 'custom_url_category' ); ?></label> | |
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value=""> | |
<p class="description"><?php _e( 'Inserisci un custom url prodotto per la categoria','custom_url_category' ); ?></p> | |
</div> | |
<?php | |
} | |
add_action( 'product_cat_add_form_fields', 'custom_url_taxonomy_add_new_meta_field', 10, 2 ); | |
// Edit term page | |
function custom_url_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" ); ?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Custom url category', 'custom_url_category' ); ?></label></th> | |
<td> | |
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>"> | |
<p class="description"><?php _e( 'Inserisci un custom url prodotto per la categoria','custom_url_category' ); ?></p> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'product_cat_edit_form_fields', 'custom_url_taxonomy_edit_meta_field', 10, 2 ); | |
// Save extra taxonomy fields callback function. | |
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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would add exactly the same secondary description field (with all text editing tools, not simple text area) but to product fields, not to product category fields.
Would you know the snippet which could make that ?
It would be amazing !