Meta Box
Author, Date,Category
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(
'author',
__( 'base', 'text_domain' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$custom_author name = get_post_meta( $post->ID, 'custom_author name', true );
$custom_post_date = get_post_meta( $post->ID, 'custom_post_date', true );
$custom_post_category = get_post_meta( $post->ID, 'custom_post_category', true );
// Set default values.
if( empty( $custom_author name ) ) $custom_author name = 'text';
if( empty( $custom_post_date ) ) $custom_post_date = 'Date Saved';
if( empty( $custom_post_category ) ) $custom_post_category = 'Category';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_author name" class="custom_author name_label">' . __( 'author', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="custom_author name" name="custom_author name" class="custom_author name_field" placeholder="' . esc_attr__( 'author', 'text_domain' ) . '">' . $custom_author name . '</textarea>';
echo ' <p class="description">' . __( 'Author Name', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_post_date" class="custom_post_date_label">' . __( 'post date', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="date" id="custom_post_date" name="custom_post_date" class="custom_post_date_field" placeholder="' . esc_attr__( 'post date', 'text_domain' ) . '" value="' . esc_attr__( $custom_post_date ) . '">';
echo ' <p class="description">' . __( 'Post Date', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_post_category" class="custom_post_category_label">' . __( 'Category', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_post_category" name="custom_post_category" class="custom_post_category_field" placeholder="' . esc_attr__( 'Category', 'text_domain' ) . '" value="' . esc_attr__( $custom_post_category ) . '">';
echo ' <p class="description">' . __( 'Post Category', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$custom_new_author name = isset( $_POST[ 'custom_author name' ] ) ? sanitize_text_field( $_POST[ 'custom_author name' ] ) : '';
$custom_new_post_date = isset( $_POST[ 'custom_post_date' ] ) ? sanitize_text_field( $_POST[ 'custom_post_date' ] ) : '';
$custom_new_post_category = isset( $_POST[ 'custom_post_category' ] ) ? sanitize_text_field( $_POST[ 'custom_post_category' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_author name', $custom_new_author name );
update_post_meta( $post_id, 'custom_post_date', $custom_new_post_date );
update_post_meta( $post_id, 'custom_post_category', $custom_new_post_category );
}
}
new Custom_Meta_Box;