Meta Embedded Child Display 1
class sqpp_metabox_embedded_child_display {
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(
'sqpp-mb-embedded-child-display',
__( 'Embedded Child Display', 'sqppcptmeta' ),
array( $this, 'render_mb_embedded_child_display' ),
'page',
'side',
'high'
);
}
public function render_mb_embedded_child_display( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nonce_action', 'nonce' );
// Retrieve an existing value from the database.
$sqpp_embedded_child_display_select = get_post_meta( $post->ID, 'sqpp_embedded_child_display_select', true );
// Set default values.
if( empty( $sqpp_embedded_child_display_select ) ) $sqpp_embedded_child_display_select = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sqpp_embedded_child_display_select" class="sqpp_embedded_child_display_select_label">' . __( 'Select', 'sqppcptmeta' ) . '</label></th>';
echo ' <td>';
echo ' <select id="sqpp_embedded_child_display_select" name="sqpp_embedded_child_display_select" class="sqpp_embedded_child_display_select_field">';
echo ' <option value="1" ' . selected( $sqpp_embedded_child_display_select, '1', false ) . '> ' . __( 'No Display', 'sqppcptmeta' ) . '</option>';
echo ' <option value="2" ' . selected( $sqpp_embedded_child_display_select, '2', false ) . '> ' . __( 'Preview + Link', 'sqppcptmeta' ) . '</option>';
echo ' <option value="3" ' . selected( $sqpp_embedded_child_display_select, '3', false ) . '> ' . __( 'Popup', 'sqppcptmeta' ) . '</option>';
echo ' <option value="4" ' . selected( $sqpp_embedded_child_display_select, '4', false ) . '> ' . __( 'Info Box', 'sqppcptmeta' ) . '</option>';
echo ' </select>';
echo ' <p class="description">' . __( 'In case this page is a child page of a parent page, then the parent page can display this page as a preview. Here you can set how the child gets displayed on its parent page in such a case.', 'sqppcptmeta' ) . '</p>';
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_sqpp_embedded_child_display_select = isset( $_POST[ 'sqpp_embedded_child_display_select' ] ) ? $_POST[ 'sqpp_embedded_child_display_select' ] : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sqpp_embedded_child_display_select', $new_sqpp_embedded_child_display_select );
}
}
new sqpp_metabox_embedded_child_display;