Advertisement
jayhoffmann_wp

Authors and Categories

Jan 9th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. function pippin_taxonomy_add_new_meta_field() {
  2.     // this will add the custom meta field to the add new term page
  3.     ?>
  4.     <div class="form-field">
  5.         <label for="term_meta[custom_term_meta]"><?php _e( 'Select Users to Include', 'pippin' ); ?></label>
  6.         <?php $authors = get_users();
  7.               foreach($authors as $author) { ?>
  8.               <input style="width:auto;" type="checkbox" name="term_meta[<?php echo $author->ID; ?>]" id="term_meta" value="<?php echo $author->ID; ?>">
  9.               <?php echo $author->user_nicename; ?><br />
  10.             <?php } ?>
  11.     </div>
  12. <?php
  13. }
  14. add_action( 'category_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
  15.  
  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.  
  24.     <tr class="form-field">
  25.     <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Select Users to Include', 'pippin' ); ?></label></th>
  26.         <td>
  27.             <?php $authors = get_users();
  28.               foreach($authors as $author) {
  29.                 $user_id = $author->ID;
  30.                if(in_array($user_id, $term_meta)) { ?>
  31.                <input type="hidden" name="term_meta[<?php echo $author->ID; ?>]" value="">
  32.               <input style="width:auto;" type="checkbox" name="term_meta[<?php echo $author->ID; ?>]" id="term_meta" value="<?php echo $author->ID; ?>" checked="checked">
  33.               <?php echo $author->user_nicename; ?><br />
  34.             <?php } else { ?>
  35.             <input style="width:auto;" type="checkbox" name="term_meta[<?php echo $author->ID; ?>]" id="term_meta" value="<?php echo $author->ID; ?>">
  36.               <?php echo $author->user_nicename; ?><br />
  37.             <?php }
  38.             } ?>
  39.         </td>
  40.     </tr>
  41. <?php
  42. }
  43. add_action( 'category_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
  44.  
  45. function save_taxonomy_custom_meta( $term_id ) {
  46.         $t_id = $term_id;
  47.         $term_meta = get_option( "taxonomy_$t_id" );
  48.         $cat_keys = array_keys( $_POST['term_meta'] );
  49.         foreach ( $cat_keys as $key ) {
  50.             if ( isset ( $_POST['term_meta'][$key] ) ) {
  51.                 $term_meta[$key] = $_POST['term_meta'][$key];
  52.             }
  53.         // Save the option array.
  54.         update_option( "taxonomy_$t_id", $term_meta );
  55.     }
  56. }  
  57. add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 );  
  58. add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement