WordPress Versions Metabox
WordPress versions data from https://codex.wordpress.org/WordPress_Versions
class Version_Info_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(
'version_info',
__( 'Version Info', 'wpinfo' ),
array( $this, 'render_metabox' ),
'version',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nonce_action', 'nonce' );
// Retrieve an existing value from the database.
$name = get_post_meta( $post->ID, 'name', true );
$musican = get_post_meta( $post->ID, 'musican', true );
$release = get_post_meta( $post->ID, 'release', true );
$blog = get_post_meta( $post->ID, 'blog', true );
$db_version = get_post_meta( $post->ID, 'db_version', true );
// Set default values.
if( empty( $name ) ) $name = '';
if( empty( $musican ) ) $musican = '';
if( empty( $release ) ) $release = '';
if( empty( $blog ) ) $blog = '';
if( empty( $db_version ) ) $db_version = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="name" class="name_label">' . __( 'Name', 'wpinfo' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="name" name="name" class="name_field" placeholder="' . esc_attr__( '', 'wpinfo' ) . '" value="' . esc_attr__( $name ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="musican" class="musican_label">' . __( 'Musican', 'wpinfo' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="musican" name="musican" class="musican_field" placeholder="' . esc_attr__( '', 'wpinfo' ) . '" value="' . esc_attr__( $musican ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="release" class="release_label">' . __( 'Release Date', 'wpinfo' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="release" name="release" class="release_field" placeholder="' . esc_attr__( 'yyyy-mm-dd hh:mm:ss', 'wpinfo' ) . '" value="' . esc_attr__( $release ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="blog" class="blog_label">' . __( 'Blog', 'wpinfo' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="blog" name="blog" class="blog_field" placeholder="' . esc_attr__( 'https://', 'wpinfo' ) . '" value="' . esc_attr__( $blog ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="db_version" class="db_version_label">' . __( 'Datebase Version', 'wpinfo' ) . '</label></th>';
echo ' <td>';
echo ' <input type="number" id="db_version" name="db_version" class="db_version_field" placeholder="' . esc_attr__( '', 'wpinfo' ) . '" value="' . esc_attr__( $db_version ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
$nonce_action = 'nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$new_name = isset( $_POST[ 'name' ] ) ? sanitize_text_field( $_POST[ 'name' ] ) : '';
$new_musican = isset( $_POST[ 'musican' ] ) ? sanitize_text_field( $_POST[ 'musican' ] ) : '';
$new_release = isset( $_POST[ 'release' ] ) ? sanitize_text_field( $_POST[ 'release' ] ) : '';
$new_blog = isset( $_POST[ 'blog' ] ) ? esc_url( $_POST[ 'blog' ] ) : '';
$new_db_version = isset( $_POST[ 'db_version' ] ) ? floatval( $_POST[ 'db_version' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'name', $new_name );
update_post_meta( $post_id, 'musican', $new_musican );
update_post_meta( $post_id, 'release', $new_release );
update_post_meta( $post_id, 'blog', $new_blog );
update_post_meta( $post_id, 'db_version', $new_db_version );
}
}
new Version_Info_Meta_Box;