Discover New Music – Band Info
class Discover_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(
'bandinfo',
__( 'Band Info', 'text_domain' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$discover_website = get_post_meta( $post->ID, 'discover_website', true );
$discover_facebook = get_post_meta( $post->ID, 'discover_facebook', true );
$discover_twitter = get_post_meta( $post->ID, 'discover_twitter', true );
$discover_instagram = get_post_meta( $post->ID, 'discover_instagram', true );
$discover_youtube = get_post_meta( $post->ID, 'discover_youtube', true );
// Set default values.
if( empty( $discover_website ) ) $discover_website = '';
if( empty( $discover_facebook ) ) $discover_facebook = '';
if( empty( $discover_twitter ) ) $discover_twitter = '';
if( empty( $discover_instagram ) ) $discover_instagram = '';
if( empty( $discover_youtube ) ) $discover_youtube = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="discover_website" class="discover_website_label">' . __( 'Website', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="discover_website" name="discover_website" class="discover_website_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $discover_website ) . '">';
echo ' <p class="description">' . __( 'Website URL', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="discover_facebook" class="discover_facebook_label">' . __( 'Facebook', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="discover_facebook" name="discover_facebook" class="discover_facebook_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $discover_facebook ) . '">';
echo ' <p class="description">' . __( 'Facebook URL', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="discover_twitter" class="discover_twitter_label">' . __( 'Twitter', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="discover_twitter" name="discover_twitter" class="discover_twitter_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $discover_twitter ) . '">';
echo ' <p class="description">' . __( 'Twitter URL', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="discover_instagram" class="discover_instagram_label">' . __( 'Instagram', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="discover_instagram" name="discover_instagram" class="discover_instagram_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $discover_instagram ) . '">';
echo ' <p class="description">' . __( 'Instagram URL', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="discover_youtube" class="discover_youtube_label">' . __( 'YouTube', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="discover_youtube" name="discover_youtube" class="discover_youtube_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $discover_youtube ) . '">';
echo ' <p class="description">' . __( 'YouTube URL', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// 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.
$discover_new_website = isset( $_POST[ 'discover_website' ] ) ? sanitize_text_field( $_POST[ 'discover_website' ] ) : '';
$discover_new_facebook = isset( $_POST[ 'discover_facebook' ] ) ? sanitize_text_field( $_POST[ 'discover_facebook' ] ) : '';
$discover_new_twitter = isset( $_POST[ 'discover_twitter' ] ) ? sanitize_text_field( $_POST[ 'discover_twitter' ] ) : '';
$discover_new_instagram = isset( $_POST[ 'discover_instagram' ] ) ? sanitize_text_field( $_POST[ 'discover_instagram' ] ) : '';
$discover_new_youtube = isset( $_POST[ 'discover_youtube' ] ) ? sanitize_text_field( $_POST[ 'discover_youtube' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'discover_website', $discover_new_website );
update_post_meta( $post_id, 'discover_facebook', $discover_new_facebook );
update_post_meta( $post_id, 'discover_twitter', $discover_new_twitter );
update_post_meta( $post_id, 'discover_instagram', $discover_new_instagram );
update_post_meta( $post_id, 'discover_youtube', $discover_new_youtube );
}
}
new Discover_Meta_Box;