Last active
August 9, 2016 09:02
-
-
Save obiPlabon/a640f99d4b1c21f7a2b72fdb164bb81d to your computer and use it in GitHub Desktop.
Snippet to register WordPress metabox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks vai