Region Term Meta
Custom term meta fields for the “Region” taxonomy.
class Region_Term_Meta { public function __construct() { if ( is_admin() ) { add_action( 'region_add_form_fields', array( $this, 'create_screen_fields'), 10, 1 ); add_action( 'region_edit_form_fields', array( $this, 'edit_screen_fields' ), 10, 2 ); add_action( 'created_region', array( $this, 'save_data' ), 10, 1 ); add_action( 'edited_region', array( $this, 'save_data' ), 10, 1 ); add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts_styles' ) ); add_action( 'admin_footer', array( $this, 'add_admin_js' ) ); } } public function create_screen_fields( $taxonomy ) { // Set default values. $region_title_color = ''; $region_title_tag = ''; $region_video = ''; // Form fields. echo '<div class="form-field term-region_title_color-wrap">'; echo ' <label for="region_title_color">' . __( 'Title Color', 'text_domain' ) . '</label>'; echo ' <input type="text" id="region_title_color" name="region_title_color" class="region_color_picker" value="' . esc_attr( $region_title_color ) . '"><br>'; echo ' <p class="description">' . __( 'The hex color of the region title.', 'text_domain' ) . '</p>'; echo '</div>'; echo '<div class="form-field term-region_title_tag-wrap">'; echo ' <label for="region_title_tag">' . __( 'Title Tag', 'text_domain' ) . '</label>'; echo ' <select id="region_title_tag" name="region_title_tag">'; echo ' <option value="h1" ' . selected( $region_title_tag, 'h1', false ) . '> ' . __( 'H1', 'text_domain' ) . '</option>'; echo ' <option value="h2" ' . selected( $region_title_tag, 'h2', false ) . '> ' . __( 'H2', 'text_domain' ) . '</option>'; echo ' <option value="h3" ' . selected( $region_title_tag, 'h3', false ) . '> ' . __( 'H3', 'text_domain' ) . '</option>'; echo ' <option value="h4" ' . selected( $region_title_tag, 'h4', false ) . '> ' . __( 'H4', 'text_domain' ) . '</option>'; echo ' <option value="h5" ' . selected( $region_title_tag, 'h5', false ) . '> ' . __( 'H5', 'text_domain' ) . '</option>'; echo ' <option value="h6" ' . selected( $region_title_tag, 'h6', false ) . '> ' . __( 'H6', 'text_domain' ) . '</option>'; echo ' </select>'; echo ' <p class="description">' . __( 'The HTML tag used to show the title.', 'text_domain' ) . '</p>'; echo '</div>'; echo '<div class="form-field term-region_video-wrap">'; echo ' <label for="region_video">' . __( 'Regiov Video', 'text_domain' ) . '</label>'; echo ' <input type="text" id="region_video" name="region_video" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $region_video ) . '">'; echo wp_oembed_get( $region_video ); echo ' <p class="description">' . __( 'Region descriptive video.', 'text_domain' ) . '</p>'; echo '</div>'; } public function edit_screen_fields( $term, $taxonomy ) { // Retrieve an existing value from the database. $region_title_color = get_term_meta( $term->term_id, 'region_title_color', true ); $region_title_tag = get_term_meta( $term->term_id, 'region_title_tag', true ); $region_video = get_term_meta( $term->term_id, 'region_video', true ); // Set default values. if( empty( $region_title_color ) ) $region_title_color = ''; if( empty( $region_title_tag ) ) $region_title_tag = ''; if( empty( $region_video ) ) $region_video = ''; // Form fields. echo '<tr class="form-field term-region_title_color-wrap">'; echo '<th scope="row">'; echo ' <label for="region_title_color">' . __( 'Title Color', 'text_domain' ) . '</label>'; echo '</th>'; echo '<td>'; echo ' <input type="text" id="region_title_color" name="region_title_color" class="region_color_picker" value="' . esc_attr( $region_title_color ) . '"><br>'; echo ' <p class="description">' . __( 'The hex color of the region title.', 'text_domain' ) . '</p>'; echo '</td>'; echo '</tr>'; echo '<tr class="form-field term-region_title_tag-wrap">'; echo '<th scope="row">'; echo ' <label for="region_title_tag">' . __( 'Title Tag', 'text_domain' ) . '</label>'; echo '</th>'; echo '<td>'; echo ' <select id="region_title_tag" name="region_title_tag">'; echo ' <option value="h1" ' . selected( $region_title_tag, 'h1', false ) . '> ' . __( 'H1', 'text_domain' ) . '</option>'; echo ' <option value="h2" ' . selected( $region_title_tag, 'h2', false ) . '> ' . __( 'H2', 'text_domain' ) . '</option>'; echo ' <option value="h3" ' . selected( $region_title_tag, 'h3', false ) . '> ' . __( 'H3', 'text_domain' ) . '</option>'; echo ' <option value="h4" ' . selected( $region_title_tag, 'h4', false ) . '> ' . __( 'H4', 'text_domain' ) . '</option>'; echo ' <option value="h5" ' . selected( $region_title_tag, 'h5', false ) . '> ' . __( 'H5', 'text_domain' ) . '</option>'; echo ' <option value="h6" ' . selected( $region_title_tag, 'h6', false ) . '> ' . __( 'H6', 'text_domain' ) . '</option>'; echo ' </select>'; echo ' <p class="description">' . __( 'The HTML tag used to show the title.', 'text_domain' ) . '</p>'; echo '</td>'; echo '</tr>'; echo '<tr class="form-field term-region_video-wrap">'; echo '<th scope="row">'; echo ' <label for="region_video">' . __( 'Regiov Video', 'text_domain' ) . '</label>'; echo '</th>'; echo '<td>'; echo ' <input type="text" id="region_video" name="region_video" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $region_video ) . '">'; echo wp_oembed_get( $region_video ); echo ' <p class="description">' . __( 'Region descriptive video.', 'text_domain' ) . '</p>'; echo '</td>'; echo '</tr>'; } public function save_data( $term_id ) { // Sanitize user input. $region_new_title_color = isset( $_POST[ 'region_title_color' ] ) ? sanitize_hex_color( $_POST[ 'region_title_color' ] ) : ''; $region_new_title_tag = isset( $_POST[ 'region_title_tag' ] ) ? $_POST[ 'region_title_tag' ] : ''; $region_new_video = isset( $_POST[ 'region_video' ] ) ? esc_url( $_POST[ 'region_video' ] ) : ''; // Update the meta field in the database. update_term_meta( $term_id, 'region_title_color', $region_new_title_color ); update_term_meta( $term_id, 'region_title_tag', $region_new_title_tag ); update_term_meta( $term_id, 'region_video', $region_new_video ); } public function load_scripts_styles() { // Color picker wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); } public function add_admin_js() { // Print js only once per page if ( did_action( 'Region_Term_Meta_js' ) >= 1 ) { return; } ?> <script type="text/javascript"> jQuery(document).ready(function($){ $('.region_color_picker').wpColorPicker(); }); </script> <?php do_action( 'Region_Term_Meta_js', $this ); } } new Region_Term_Meta;