Advertisement
Guest User

Matt code snippet

a guest
May 16th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. // Add term page
  2. function pippin_taxonomy_add_new_meta_field() {
  3. // this will add the custom meta field to the add new term page
  4. ?>
  5. <div class="form-field">
  6. <label for="term_meta[caturl]"><?php _e( 'Category Archive URL', 'pippin' ); ?></label>
  7. <input type="text" name="term_meta[caturl]" id="term_meta[caturl]" value="">
  8. <p class="description"><?php _e( 'Add URL for category archive','pippin' ); ?></p>
  9. </div>
  10. <?php
  11. }
  12. add_action( 'category_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
  13.  
  14.  
  15. // Edit term page
  16. function pippin_taxonomy_edit_meta_field($term) {
  17.  
  18. // put the term ID into a variable
  19. $t_id = $term->term_id;
  20.  
  21. // retrieve the existing value(s) for this meta field. This returns an array
  22. $term_meta = get_option( "taxonomy_$t_id" ); ?>
  23. <tr class="form-field">
  24. <th scope="row" valign="top"><label for="term_meta[caturl]"><?php _e( 'Category Archive URL', 'pippin' ); ?></label></th>
  25. <td>
  26. <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['caturl'] ) ? esc_attr( $term_meta['caturl'] ) : ''; ?>">
  27. <p class="description"><?php _e( 'Add URL for category archive','pippin' ); ?></p>
  28. </td>
  29. </tr>
  30. <?php
  31. }
  32. add_action( 'category_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
  33.  
  34. // Save extra taxonomy fields callback function.
  35. function save_taxonomy_custom_meta( $term_id ) {
  36. if ( isset( $_POST['term_meta'] ) ) {
  37. $t_id = $term_id;
  38. $term_meta = get_option( "taxonomy_$t_id" );
  39. $cat_keys = array_keys( $_POST['term_meta'] );
  40. foreach ( $cat_keys as $key ) {
  41. if ( isset ( $_POST['term_meta'][$key] ) ) {
  42. $term_meta[$key] = $_POST['term_meta'][$key];
  43. }
  44. }
  45. // Save the option array.
  46. update_option( "taxonomy_$t_id", $term_meta );
  47. }
  48. }
  49. add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 );
  50. add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement