Custom Meta for ERO Taxonomy
class ero_tax_custom_meta {
public function __construct() {
if ( is_admin() ) {
add_action( 'ero-tax_add_form_fields', array( $this, 'create_screen_fields'), 10, 1 );
add_action( 'ero-tax_edit_form_fields', array( $this, 'edit_screen_fields' ), 10, 2 );
add_action( 'created_ero-tax', array( $this, 'save_data' ), 10, 1 );
add_action( 'edited_ero-tax', 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_ero_metaero-date = '';
$custom_ero_metaero_url = '';
$custom_ero_metaero_headnote = '';
$custom_ero_metaero_alp = '';
// Form fields.
echo '<div class="form-field term-custom_ero_metaero-date-wrap">';
echo ' <label for="custom_ero_metaero-date">' . __( 'Ero Date', 'text_domain' ) . '</label>';
echo ' <input type="text" id="custom_ero_metaero-date" name="custom_ero_metaero-date" class="widefat custom_ero_metadate_picker" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $custom_ero_metaero-date ) . '">';
echo '</div>';
echo '<div class="form-field term-custom_ero_metaero_url-wrap">';
echo ' <label for="custom_ero_metaero_url">' . __( 'Ero URL', 'text_domain' ) . '</label>';
echo ' <input type="url" id="custom_ero_metaero_url" name="custom_ero_metaero_url" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $custom_ero_metaero_url ) . '">';
echo '</div>';
echo '<div class="form-field term-custom_ero_metaero_headnote-wrap">';
echo ' <label for="custom_ero_metaero_headnote">' . __( 'Ero Headnote', 'text_domain' ) . '</label>';
echo ' <textarea id="custom_ero_metaero_headnote" name="custom_ero_metaero_headnote" placeholder="' . esc_attr__( '', 'text_domain' ) . '">' . $custom_ero_metaero_headnote . '</textarea>';
echo ' <p class="description">' . __( 'from OSC', 'text_domain' ) . '</p>';
echo '</div>';
echo '<div class="form-field term-custom_ero_metaero_alp-wrap">';
echo ' <label for="custom_ero_metaero_alp">' . __( 'Ero ALP', 'text_domain' ) . '</label>';
echo ' <input type="text" id="custom_ero_metaero_alp" name="custom_ero_metaero_alp" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $custom_ero_metaero_alp ) . '">';
echo ' <p class="description">' . __( 'applicable legislative provisions', 'text_domain' ) . '</p>';
echo '</div>';
}
public function edit_screen_fields( $term, $taxonomy ) {
// Retrieve an existing value from the database.
$custom_ero_metaero-date = get_term_meta( $term->term_id, 'custom_ero_metaero-date', true );
$custom_ero_metaero_url = get_term_meta( $term->term_id, 'custom_ero_metaero_url', true );
$custom_ero_metaero_headnote = get_term_meta( $term->term_id, 'custom_ero_metaero_headnote', true );
$custom_ero_metaero_alp = get_term_meta( $term->term_id, 'custom_ero_metaero_alp', true );
// Set default values.
if( empty( $custom_ero_metaero-date ) ) $custom_ero_metaero-date = '';
if( empty( $custom_ero_metaero_url ) ) $custom_ero_metaero_url = '';
if( empty( $custom_ero_metaero_headnote ) ) $custom_ero_metaero_headnote = '';
if( empty( $custom_ero_metaero_alp ) ) $custom_ero_metaero_alp = '';
// Form fields.
echo '<tr class="form-field term-custom_ero_metaero-date-wrap">';
echo '<th scope="row">';
echo ' <label for="custom_ero_metaero-date">' . __( 'Ero Date', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="custom_ero_metaero-date" name="custom_ero_metaero-date" class="widefat custom_ero_metadate_picker" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $custom_ero_metaero-date ) . '">';
echo '</td>';
echo '</tr>';
echo '<tr class="form-field term-custom_ero_metaero_url-wrap">';
echo '<th scope="row">';
echo ' <label for="custom_ero_metaero_url">' . __( 'Ero URL', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="url" id="custom_ero_metaero_url" name="custom_ero_metaero_url" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $custom_ero_metaero_url ) . '">';
echo '</td>';
echo '</tr>';
echo '<tr class="form-field term-custom_ero_metaero_headnote-wrap">';
echo '<th scope="row">';
echo ' <label for="custom_ero_metaero_headnote">' . __( 'Ero Headnote', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <textarea id="custom_ero_metaero_headnote" name="custom_ero_metaero_headnote" placeholder="' . esc_attr__( '', 'text_domain' ) . '">' . $custom_ero_metaero_headnote . '</textarea>';
echo ' <p class="description">' . __( 'from OSC', 'text_domain' ) . '</p>';
echo '</td>';
echo '</tr>';
echo '<tr class="form-field term-custom_ero_metaero_alp-wrap">';
echo '<th scope="row">';
echo ' <label for="custom_ero_metaero_alp">' . __( 'Ero ALP', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="custom_ero_metaero_alp" name="custom_ero_metaero_alp" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $custom_ero_metaero_alp ) . '">';
echo ' <p class="description">' . __( 'applicable legislative provisions', 'text_domain' ) . '</p>';
echo '</td>';
echo '</tr>';
}
public function save_data( $term_id ) {
// Sanitize user input.
$custom_ero_metanew_ero-date = isset( $_POST[ 'custom_ero_metaero-date' ] ) ? sanitize_text_field( $_POST[ 'custom_ero_metaero-date' ] ) : '';
$custom_ero_metanew_ero_url = isset( $_POST[ 'custom_ero_metaero_url' ] ) ? esc_url( $_POST[ 'custom_ero_metaero_url' ] ) : '';
$custom_ero_metanew_ero_headnote = isset( $_POST[ 'custom_ero_metaero_headnote' ] ) ? sanitize_text_field( $_POST[ 'custom_ero_metaero_headnote' ] ) : '';
$custom_ero_metanew_ero_alp = isset( $_POST[ 'custom_ero_metaero_alp' ] ) ? sanitize_text_field( $_POST[ 'custom_ero_metaero_alp' ] ) : '';
// Update the meta field in the database.
update_term_meta( $term_id, 'custom_ero_metaero-date', $custom_ero_metanew_ero-date );
update_term_meta( $term_id, 'custom_ero_metaero_url', $custom_ero_metanew_ero_url );
update_term_meta( $term_id, 'custom_ero_metaero_headnote', $custom_ero_metanew_ero_headnote );
update_term_meta( $term_id, 'custom_ero_metaero_alp', $custom_ero_metanew_ero_alp );
}
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( 'ero_tax_custom_meta_js' ) >= 1 ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.custom_ero_metadate_picker').datepicker();
});
</script>
<?php
do_action( 'ero_tax_custom_meta_js', $this );
}
}
new ero_tax_custom_meta;