Magazine issue extra data
Magazine issue release date and cover image.
class Magazine_Issue_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.
$gwp_issues_date = '';
$gwp_cover_image = '';
// Form fields.
echo '<div class="form-field term-gwp_issues_date-wrap">';
echo ' <label for="gwp_issues_date">' . __( 'Issues Date', 'text_domain' ) . '</label>';
echo ' <input type="text" id="gwp_issues_date" name="gwp_issues_date" class="widefat gwp_date_picker" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $gwp_issues_date ) . '">';
echo ' <p class="description">' . __( 'Magazine issue release date.', 'text_domain' ) . '</p>';
echo '</div>';
echo '<div class="form-field term-gwp_cover_image-wrap">';
echo ' <label for="gwp_cover_image">' . __( 'Cover Image', 'text_domain' ) . '</label>';
echo ' <input type="url" id="gwp_cover_image" name="gwp_cover_image" placeholder="' . esc_attr__( 'https://', 'text_domain' ) . '" value="' . esc_attr( $gwp_cover_image ) . '">';
echo ' <p class="description">' . __( 'URL of the cover image.', 'text_domain' ) . '</p>';
echo '</div>';
}
public function edit_screen_fields( $term, $taxonomy ) {
// Retrieve an existing value from the database.
$gwp_issues_date = get_term_meta( $term->term_id, 'gwp_issues_date', true );
$gwp_cover_image = get_term_meta( $term->term_id, 'gwp_cover_image', true );
// Set default values.
if( empty( $gwp_issues_date ) ) $gwp_issues_date = '';
if( empty( $gwp_cover_image ) ) $gwp_cover_image = '';
// Form fields.
echo '<tr class="form-field term-gwp_issues_date-wrap">';
echo '<th scope="row">';
echo ' <label for="gwp_issues_date">' . __( 'Issues Date', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="gwp_issues_date" name="gwp_issues_date" class="widefat gwp_date_picker" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $gwp_issues_date ) . '">';
echo ' <p class="description">' . __( 'Magazine issue release date.', 'text_domain' ) . '</p>';
echo '</td>';
echo '</tr>';
echo '<tr class="form-field term-gwp_cover_image-wrap">';
echo '<th scope="row">';
echo ' <label for="gwp_cover_image">' . __( 'Cover Image', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="url" id="gwp_cover_image" name="gwp_cover_image" placeholder="' . esc_attr__( 'https://', 'text_domain' ) . '" value="' . esc_attr( $gwp_cover_image ) . '">';
echo ' <p class="description">' . __( 'URL of the cover image.', 'text_domain' ) . '</p>';
echo '</td>';
echo '</tr>';
}
public function save_data( $term_id ) {
// Sanitize user input.
$gwp_new_issues_date = isset( $_POST[ 'gwp_issues_date' ] ) ? sanitize_text_field( $_POST[ 'gwp_issues_date' ] ) : '';
$gwp_new_cover_image = isset( $_POST[ 'gwp_cover_image' ] ) ? esc_url( $_POST[ 'gwp_cover_image' ] ) : '';
// Update the meta field in the database.
update_term_meta( $term_id, 'gwp_issues_date', $gwp_new_issues_date );
update_term_meta( $term_id, 'gwp_cover_image', $gwp_new_cover_image );
}
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( 'Magazine_Issue_Term_Meta_js' ) >= 1 ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.gwp_date_picker').datepicker();
});
</script>
<?php
do_action( 'Magazine_Issue_Term_Meta_js', $this );
}
}
new Magazine_Issue_Term_Meta;