Skip to content

Instantly share code, notes, and snippets.

@turtlepod
Created May 29, 2020 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlepod/0a9d118e8657770b2a68907d65962240 to your computer and use it in GitHub Desktop.
Save turtlepod/0a9d118e8657770b2a68907d65962240 to your computer and use it in GitHub Desktop.
<?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