Untitled Snippet
class Program_Meta {
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(
'program-meta',
__( 'Program Information', 'omd-programs' ),
array( $this, 'render_metabox' ),
'omd-programs',
'side',
'high'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'omdp_nonce_action', 'omdp_nonce' );
// Retrieve an existing value from the database.
$omdp_learn_more_link = get_post_meta( $post->ID, 'omdp_learn_more_link', true );
$omdp_logged_in_only = get_post_meta( $post->ID, 'omdp_logged_in_only', true );
// Set default values.
if( empty( $omdp_learn_more_link ) ) $omdp_learn_more_link = '';
if( empty( $omdp_logged_in_only ) ) $omdp_logged_in_only = '1';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="omdp_learn_more_link" class="omdp_learn_more_link_label">' . __( 'URL for "Learn More" Button', 'omd-programs' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="omdp_learn_more_link" name="omdp_learn_more_link" class="omdp_learn_more_link_field" placeholder="' . esc_attr__( '', 'omd-programs' ) . '" value="' . esc_attr( $omdp_learn_more_link ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="omdp_logged_in_only" class="omdp_logged_in_only_label">' . __( 'User must be logged in to request program?', 'omd-programs' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" id="omdp_logged_in_only" name="omdp_logged_in_only" class="omdp_logged_in_only_field" value="checked" ' . checked( $omdp_logged_in_only, 'checked', false ) . '> ' . __( '', 'omd-programs' ) . '</label>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['omdp_nonce'] ) ? $_POST['omdp_nonce'] : '';
$nonce_action = 'omdp_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;
// Sanitize user input.
$omdp_new_learn_more_link = isset( $_POST[ 'omdp_learn_more_link' ] ) ? esc_url( $_POST[ 'omdp_learn_more_link' ] ) : '';
$omdp_new_logged_in_only = isset( $_POST[ 'omdp_logged_in_only' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'omdp_learn_more_link', $omdp_new_learn_more_link );
update_post_meta( $post_id, 'omdp_logged_in_only', $omdp_new_logged_in_only );
}
}
new Program_Meta;