City Term Meta
class City_Term_Meta {
public function __construct() {
if ( is_admin() ) {
add_action( 'city_add_form_fields', array( $this, 'create_screen_fields'), 10, 1 );
add_action( 'city_edit_form_fields', array( $this, 'edit_screen_fields' ), 10, 2 );
add_action( 'created_city', array( $this, 'save_data' ), 10, 1 );
add_action( 'edited_city', array( $this, 'save_data' ), 10, 1 );
}
}
public function create_screen_fields( $taxonomy ) {
// Set default values.
$city_custom_ = '';
$city_custom_ = '';
$city_custom_ = '';
// Form fields.
echo '<div class="form-field term-city_custom_-wrap">';
echo ' <label for="city_custom_">' . __( 'State', 'text_domain' ) . '</label>';
echo ' <input type="text" id="city_custom_" name="city_custom_" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $city_custom_ ) . '">';
echo '</div>';
echo '<div class="form-field term-city_custom_-wrap">';
echo ' <label for="city_custom_">' . __( 'State Url', 'text_domain' ) . '</label>';
echo ' <input type="text" id="city_custom_" name="city_custom_" placeholder="' . esc_attr__( 'state-name', 'text_domain' ) . '" value="' . esc_attr( $city_custom_ ) . '">';
echo '</div>';
echo '<div class="form-field term-city_custom_-wrap">';
echo ' <label for="city_custom_">' . __( 'State Abbr URL', 'text_domain' ) . '</label>';
echo ' <input type="text" id="city_custom_" name="city_custom_" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $city_custom_ ) . '">';
echo '</div>';
}
public function edit_screen_fields( $term, $taxonomy ) {
// Retrieve an existing value from the database.
$city_custom_ = get_term_meta( $term->term_id, 'city_custom_', true );
$city_custom_ = get_term_meta( $term->term_id, 'city_custom_', true );
$city_custom_ = get_term_meta( $term->term_id, 'city_custom_', true );
// Set default values.
if( empty( $city_custom_ ) ) $city_custom_ = '';
if( empty( $city_custom_ ) ) $city_custom_ = '';
if( empty( $city_custom_ ) ) $city_custom_ = '';
// Form fields.
echo '<tr class="form-field term-city_custom_-wrap">';
echo '<th scope="row">';
echo ' <label for="city_custom_">' . __( 'State', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="city_custom_" name="city_custom_" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $city_custom_ ) . '">';
echo '</td>';
echo '</tr>';
echo '<tr class="form-field term-city_custom_-wrap">';
echo '<th scope="row">';
echo ' <label for="city_custom_">' . __( 'State Url', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="city_custom_" name="city_custom_" placeholder="' . esc_attr__( 'state-name', 'text_domain' ) . '" value="' . esc_attr( $city_custom_ ) . '">';
echo '</td>';
echo '</tr>';
echo '<tr class="form-field term-city_custom_-wrap">';
echo '<th scope="row">';
echo ' <label for="city_custom_">' . __( 'State Abbr URL', 'text_domain' ) . '</label>';
echo '</th>';
echo '<td>';
echo ' <input type="text" id="city_custom_" name="city_custom_" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $city_custom_ ) . '">';
echo '</td>';
echo '</tr>';
}
public function save_data( $term_id ) {
// Sanitize user input.
$city_custom_new_ = isset( $_POST[ 'city_custom_' ] ) ? sanitize_text_field( $_POST[ 'city_custom_' ] ) : '';
$city_custom_new_ = isset( $_POST[ 'city_custom_' ] ) ? sanitize_text_field( $_POST[ 'city_custom_' ] ) : '';
$city_custom_new_ = isset( $_POST[ 'city_custom_' ] ) ? sanitize_text_field( $_POST[ 'city_custom_' ] ) : '';
// Update the meta field in the database.
update_term_meta( $term_id, 'city_custom_', $city_custom_new_ );
update_term_meta( $term_id, 'city_custom_', $city_custom_new_ );
update_term_meta( $term_id, 'city_custom_', $city_custom_new_ );
}
}
new City_Term_Meta;