Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
add_action( 'init', function() {
// Add field:
add_action( 'add_meta_boxes', function() {
add_meta_box(
'my_meta_box',
'My Meta Box',
function( $post ) {
wp_nonce_field( __FILE__, '_my_data_nonce' );
?>
<p><input type="text" class="large-text" name="my_data" value="<?php echo esc_attr( get_post_meta( $post->ID, '_my_data', true ) ); ?>"></p>
<?php
},
'post',
'side'
);
} );
// Save field.
add_action( 'save_post', function( $post_id ) {
if ( isset( $_POST['my_data'], $_POST['_my_data_nonce'] ) && wp_verify_nonce( $_POST['_my_data_nonce'], __FILE__ ) ) {
update_post_meta( $post_id, '_my_data', sanitize_text_field( $_POST['my_data'] ) );
}
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment