This will add a field called ‘TERM META TEXT’ to your categories. I did take out the nonce but I really think it should go back in. Also, it’s just better to have some sanitization vs. none. This example includes javascript and CSS hooks which you may or may not need but you can quickly see how all the parts go together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | // REGISTER TERM META add_action( 'init', '___register_term_meta_text' ); function ___register_term_meta_text() { register_meta( 'term', '__term_meta_text', '___sanitize_term_meta_text' ); } // SANITIZE DATA function ___sanitize_term_meta_text ( $value ) { return sanitize_text_field ($value); } // GETTER (will be sanitized) function ___get_term_meta_text( $term_id ) { $value = get_term_meta( $term_id, '__term_meta_text', true ); $value = ___sanitize_term_meta_text( $value ); return $value; } // ADD FIELD TO CATEGORY TERM PAGE add_action( 'category_add_form_fields', '___add_form_field_term_meta_text' ); function ___add_form_field_term_meta_text() { ?> <?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?> <div class="form-field term-meta-text-wrap"> <label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label> <input type="text" name="term_meta_text" id="term-meta-text" value="" class="term-meta-text-field" /> </div> <?php } // ADD FIELD TO CATEGORY EDIT PAGE add_action( 'category_edit_form_fields', '___edit_form_field_term_meta_text' ); function ___edit_form_field_term_meta_text( $term ) { $value = ___get_term_meta_text( $term->term_id ); if ( ! $value ) $value = ""; ?> <tr class="form-field term-meta-text-wrap"> <th scope="row"><label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label></th> <td> <?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?> <input type="text" name="term_meta_text" id="term-meta-text" value="<?php echo esc_attr( $value ); ?>" class="term-meta-text-field" /> </td> </tr> <?php } // SAVE TERM META (on term edit & create) add_action( 'edit_category', '___save_term_meta_text' ); add_action( 'create_category', '___save_term_meta_text' ); function ___save_term_meta_text( $term_id ) { // verify the nonce --- remove if you don't care if ( ! isset( $_POST['term_meta_text_nonce'] ) || ! wp_verify_nonce( $_POST['term_meta_text_nonce'], basename( __FILE__ ) ) ) return; $old_value = ___get_term_meta_text( $term_id ); $new_value = isset( $_POST['term_meta_text'] ) ? ___sanitize_term_meta_text ( $_POST['term_meta_text'] ) : ''; if ( $old_value && '' === $new_value ) delete_term_meta( $term_id, '__term_meta_text' ); else if ( $old_value !== $new_value ) update_term_meta( $term_id, '__term_meta_text', $new_value ); } // MODIFY COLUMNS (add our meta to the list) add_filter( 'manage_edit-category_columns', '___edit_term_columns' ); function ___edit_term_columns( $columns ) { $columns['__term_meta_text'] = __( 'TERM META TEXT', 'text_domain' ); return $columns; } // RENDER COLUMNS (render the meta data on a column) add_filter( 'manage_category_custom_column', '___manage_term_custom_column', 10, 3 ); function ___manage_term_custom_column( $out, $column, $term_id ) { if ( '__term_meta_text' === $column ) { $value = ___get_term_meta_text( $term_id ); if ( ! $value ) $value = ''; $out = sprintf( '<span class="term-meta-text-block" style="" >%s</div>', esc_attr( $value ) ); } return $out; } // ADD JAVASCRIPT & STYLES TO COLUMNS add_action( 'admin_enqueue_scripts', '___admin_enqueue_scripts' ); function ___admin_enqueue_scripts( $hook_suffix ) { if ( 'edit-tags.php' !== $hook_suffix || 'category' !== get_current_screen()->taxonomy ) return; // ADD YOUR SUPPORTING CSS / JS FILES HERE // wp_enqueue_style( 'wp-color-picker' ); // wp_enqueue_script( 'wp-color-picker' ); add_action( 'admin_head', '___meta_term_text_print_styles' ); add_action( 'admin_footer', '___meta_term_text_print_scripts' ); } // PRINT OUR CUSTOM STYLES function ___meta_term_text_print_styles() { ?> <style type="text/css"> .column-__term_meta_text { background-color:rgb(249, 249, 249); border: 1px solid lightgray;} .column-__term_meta_text .term-meta-text-block { display: inline-block; color:darkturquoise; } </style> <?php } // PRINT OUR CUSTOM SCRIPTS |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.