Skip to content

Instantly share code, notes, and snippets.

@obiPlabon
Last active August 9, 2016 09:02
Show Gist options
  • Save obiPlabon/a640f99d4b1c21f7a2b72fdb164bb81d to your computer and use it in GitHub Desktop.
Save obiPlabon/a640f99d4b1c21f7a2b72fdb164bb81d to your computer and use it in GitHub Desktop.
Snippet to register WordPress metabox
<?php
function wp_class_register_mb() {
add_meta_box(
'wpclass-mb', // unique id
esc_html__( 'WP Class Metabox', 'text-domain' ), // metabox title
'wp_class_mb_cb', // metabox callback
array('post','page'), // screen name [string|array]
'side', // context
'high' // priority
);
}
add_action( 'add_meta_boxes', 'wp_class_register_mb' );
function wp_class_mb_cb( $post ) {
$data = get_post_meta($post->ID, 'wp_class_meta_data', true);
?>
<input type="text" name="wp_class_input" value="<?php echo esc_attr( $data ); ?>">
<?php
}
function wp_class_save_meta_data($post_id) {
$data = isset( $_POST['wp_class_input'] ) ? $_POST['wp_class_input'] : '';
if ( $data ) {
update_post_meta( $post_id, 'wp_class_meta_data', $data );
}
}
add_action( 'save_post', 'wp_class_save_meta_data' );
@polashrp
Copy link

polashrp commented Aug 9, 2016

Thanks vai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment