Radio metabox
class page_metabox {
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(
'page_metaboxe',
__( 'Advanced Options', 'moreshet-ladino' ),
array( $this, 'render_metabox' ),
'page',
'side',
'high'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$sg_sidebar_right_book = get_post_meta( $post->ID, 'sg_sidebar_right_book', true );
// Set default values.
if( empty( $sg_sidebar_right_book ) ) $sg_sidebar_right_book = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sg_sidebar_right_book" class="sg_sidebar_right_book_label">' . __( 'Style', 'moreshet-ladino' ) . '</label></th>';
echo ' <td>';
echo ' <input type="radio" name="sg_sidebar_right_book" class="sg_sidebar_right_book_field" value="sg_regular " ' . checked( $sg_sidebar_right_book, 'sg_regular ', false ) . '> ' . __( ' Regular', 'moreshet-ladino' ) . '<br>';
echo ' <input type="radio" name="sg_sidebar_right_book" class="sg_sidebar_right_book_field" value="sg_sidebar_right_book " ' . checked( $sg_sidebar_right_book, 'sg_sidebar_right_book ', false ) . '> ' . __( ' Sidebar Right', 'moreshet-ladino' ) . '<br>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$sg_new_sidebar_right_book = isset( $_POST[ 'sg_sidebar_right_book' ] ) ? $_POST[ 'sg_sidebar_right_book' ] : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sg_sidebar_right_book', $sg_new_sidebar_right_book );
}
}
new page_metabox;