fields
class Custom_Meta_Box {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'',
__( 'Options', 'text_domain' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$custom_ = get_post_meta( $post->ID, 'custom_', true );
$custom_ = get_post_meta( $post->ID, 'custom_', true );
$custom_ = get_post_meta( $post->ID, 'custom_', true );
// Set default values.
if( empty( $custom_ ) ) $custom_ = '';
if( empty( $custom_ ) ) $custom_ = 'Baton Rouge';
if( empty( $custom_ ) ) $custom_ = '225-768-2050';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_" class="custom__label">' . __( 'Floor', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_" name="custom_" class="custom__field" placeholder="' . esc_attr__( 'Floor', 'text_domain' ) . '" value="' . esc_attr( $custom_ ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_" class="custom__label">' . __( 'Appointment Locations', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="custom_" name="custom_" class="custom__field" placeholder="' . esc_attr__( '', 'text_domain' ) . '">' . $custom_ . '</textarea>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_" class="custom__label">' . __( 'Phone', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="custom_" name="custom_" class="custom__field" placeholder="' . esc_attr__( '', 'text_domain' ) . '">' . $custom_ . '</textarea>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$custom_new_ = isset( $_POST[ 'custom_' ] ) ? sanitize_text_field( $_POST[ 'custom_' ] ) : '';
$custom_new_ = isset( $_POST[ 'custom_' ] ) ? sanitize_text_field( $_POST[ 'custom_' ] ) : '';
$custom_new_ = isset( $_POST[ 'custom_' ] ) ? sanitize_text_field( $_POST[ 'custom_' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_', $custom_new_ );
update_post_meta( $post_id, 'custom_', $custom_new_ );
update_post_meta( $post_id, 'custom_', $custom_new_ );
}
}
new Custom_Meta_Box;