Advertisement
thetareq

Custom meta field in product category page

Mar 30th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. //Product Cat Create page
  2. function taxonomy_add_new_meta_field() {
  3.     ?>
  4.    
  5.     <div class="form-field">
  6.         <label for="meta-field"><?php echo __( 'Extra Meta Field', 'woocommerce' ); ?></label>
  7.        
  8.       <?php
  9.       $name = 'meta-field';      
  10.       $settings =   array(
  11.             'wpautop' => true,              
  12.             'media_buttons' => true,        
  13.             'textarea_name' => $name,      
  14.             'textarea_rows' => 10,          
  15.             'tabindex' => '',              
  16.             'editor_css' => '',            
  17.             'teeny' => false,              
  18.             'dfw' => false,                
  19.             'tinymce' => true,              
  20.             'quicktags' => true,            
  21.             'drag_drop_upload' => false    
  22.         );
  23.       wp_editor( '', $name, $settings );
  24.       ?>
  25.        </div>      
  26.     <?php
  27. }
  28.  
  29. //Product Cat Edit page
  30. function taxonomy_add_edit_meta_field($term) {
  31.  
  32.     //getting term ID
  33.     $term_id = $term->term_id;
  34.     // retrieve the existing value(s) for this meta field.
  35.    $meta_tynmc = get_term_meta($term_id, 'meta-field', true);
  36.     ?>
  37.  
  38.     <tr class="form-field">
  39.         <th scope="row" valign="top"><label for="meta-field"><?php echo __( 'Extra Meta Field', 'woocommerce' ); ?></label></th>
  40.         <td>
  41.             <?php                  
  42.     $name = 'meta-field';      
  43.       $settings =   array(
  44.             'wpautop' => true,            
  45.             'media_buttons' => true,        
  46.             'textarea_name' => $name,      
  47.             'textarea_rows' => 10,          
  48.             'tabindex' => '',              
  49.             'editor_css' => '',            
  50.             'editor_class' => '',          
  51.             'teeny' => false,              
  52.             'dfw' => false,                
  53.             'tinymce' => true,              
  54.             'quicktags' => true,            
  55.             'drag_drop_upload' => false    
  56.         );
  57.       wp_editor( $meta_tynmc, $name, $settings );
  58.       ?>
  59.         </td>      
  60.     </tr>
  61.      
  62.     <?php
  63. }
  64.  
  65. add_action('product_cat_add_form_fields', 'taxonomy_add_new_meta_field', 10, 1);
  66. add_action('product_cat_edit_form_fields', 'taxonomy_add_edit_meta_field', 10, 1);
  67.  
  68. // Save extra taxonomy fields callback function.
  69. function save_taxonomy_custom_meta($term_id) {
  70.     $tyn = filter_input(INPUT_POST, 'meta-field');
  71.     update_term_meta($term_id, 'meta-field', $tyn);
  72. }
  73.  
  74. add_action('edited_product_cat', 'save_taxonomy_custom_meta', 10, 1);
  75. add_action('create_product_cat', 'save_taxonomy_custom_meta', 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement