Term Meta (Date)
class Custom_Term_Meta {
public function __construct() {
if ( is_admin() ) {
add_action( 'issue_add_form_fields', array( $this, 'create_screen_fields'), 10, 1 );
add_action( 'issue_edit_form_fields', array( $this, 'edit_screen_fields' ), 10, 2 );
add_action( 'created_issue', array( $this, 'save_data' ), 10, 1 );
add_action( 'edited_issue', 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.
$custom_ = '';
// Form fields.
echo '<div class="form-field term-custom_-wrap">';
echo ' <label for="custom_">' . __( 'Issue Date', 'mch-core' ) . '</label>';
echo ' <input type="text" id="custom_" name="custom_" class="widefat custom_date_picker" placeholder="' . esc_attr__( '07/15/2018', 'mch-core' ) . '" value="' . esc_attr( $custom_ ) . '">';
echo ' <p class="description">' . __( 'Enter the date of Issue', 'mch-core' ) . '</p>';
echo '</div>';
}
public function edit_screen_fields( $term, $taxonomy ) {
// Retrieve an existing value from the database.
$custom_ = get_term_meta( $term->term_id, 'custom_', true );
// Set default values.
if( empty( $custom_ ) ) $custom_ = '';
// Form fields.
echo '<tr class="form-field term-custom_-wrap">';
echo '<th scope="row">';
echo ' <label for="custom_">' . __( 'Issue Date', 'mch-core' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="custom_" name="custom_" class="widefat custom_date_picker" placeholder="' . esc_attr__( '07/15/2018', 'mch-core' ) . '" value="' . esc_attr( $custom_ ) . '">';
echo ' <p class="description">' . __( 'Enter the date of Issue', 'mch-core' ) . '</p>';
echo '</td>';
echo '</tr>';
}
public function save_data( $term_id ) {
// Sanitize user input.
$custom_new_ = isset( $_POST[ 'custom_' ] ) ? sanitize_text_field( $_POST[ 'custom_' ] ) : '';
// Update the meta field in the database.
update_term_meta( $term_id, 'custom_', $custom_new_ );
}
public function load_scripts_styles() {
// Date picker
wp_enqueue_script( 'jquery-ui' );
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_style( 'jquery-ui-datepicker', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css' );
}
public function add_admin_js() {
// Print js only once per page
if ( did_action( 'Custom_Term_Meta_js' ) >= 1 ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.custom_date_picker').datepicker();
});
</script>
<?php
do_action( 'Custom_Term_Meta_js', $this );
}
}
new Custom_Term_Meta;