Skip to content

Instantly share code, notes, and snippets.

@codeagencybe
Created August 10, 2020 20:17
Show Gist options
  • Save codeagencybe/f61af542fbbbf368ad13a7c79dd84e35 to your computer and use it in GitHub Desktop.
Save codeagencybe/f61af542fbbbf368ad13a7c79dd84e35 to your computer and use it in GitHub Desktop.
custom fields woocommerce category
// Add term page
function codeagency_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[en_translation]"><?php _e( 'en_translation', 'codagency' ); ?></label>
<input type="text" name="term_meta[en_translation]" id="term_meta[en_translation]" value="">
</div>
<div class="form-field">
<label for="term_meta[fr_translation]"><?php _e( 'fr_translation', 'codagency' ); ?></label>
<input type="text" name="term_meta[fr_translation]" id="term_meta[fr_translation]" value="">
</div>
<div class="form-field">
<label for="term_meta[de_translation]"><?php _e( 'de_translation', 'codagency' ); ?></label>
<input type="text" name="term_meta[de_translation]" id="term_meta[de_translation]" value="">
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'codeagency_taxonomy_add_new_meta_field', 10, 2 );
//Product Cat Edit page
function codeagency_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option("taxonomy_" . $term_id);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[en_translation]"><?php _e('en_translation', 'codeagency'); ?></label></th>
<td>
<input type="text" name="term_meta[en_translation]" id="term_meta[en_translation]" value="<?php echo esc_attr($term_meta['en_translation']) ? esc_attr($term_meta['en_translation']) : ''; ?>">
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[fr_translation]"><?php _e('fr_translation', 'codeagency'); ?></label></th>
<td>
<input type="text" name="term_meta[fr_translation]" id="term_meta[fr_translation]" value="<?php echo esc_attr($term_meta['fr_translation']) ? esc_attr($term_meta['fr_translation']) : ''; ?>">
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[de_translation]"><?php _e('de_translation', 'codeagency'); ?></label></th>
<td>
<input type="text" name="term_meta[de_translation]" id="term_meta[de_translation]" value="<?php echo esc_attr($term_meta['de_translation']) ? esc_attr($term_meta['de_translation']) : ''; ?>">
</td>
</tr>
<?php
}
add_action('product_cat_edit_form_fields', 'codeagency_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta($term_id) {
if (isset($_POST['term_meta'])) {
$term_meta = get_option("taxonomy_" . $term_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_" . $term_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