Skip to content

Instantly share code, notes, and snippets.

@picocodes
Created July 27, 2022 07:16
Show Gist options
  • Save picocodes/6694b91241708dbaff060b32f66e6ec1 to your computer and use it in GitHub Desktop.
Save picocodes/6694b91241708dbaff060b32f66e6ec1 to your computer and use it in GitHub Desktop.
<?php
/**
* Creates a custom meta box.
*
* @param string $post_type Post type.
* @param WP_Post $post Post.
*/
function prefix_add_meta_box( $post_type, $post ) {
// If you want to add the meta box to all post types, remove this line.
if ( 'my_custom_post_type' !== $post_type ) {
return;
}
add_meta_box(
'my_custom_meta_box', // ID. Should be unique.
__( 'Custom Metabox', 'my-text-domain' ), // Title.
'prefix_meta_box_callback', // Display the meta box in this function.
'my_screen_id', // The screen id.
'side', // can be 'normal', 'advanced', or 'side'.
'high' // can be 'high', 'default' or 'low'.
);
}
add_action( 'add_meta_boxes', 'prefix_add_meta_box', 10, 2 );
/**
* Display the meta box.
*
* @param WP_Post $post Post.
*/
function prefix_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'my_meta_box_nonce', 'prefix_meta_box_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
// Display the form fields.
?>
<p>
<label for="my_meta_value"><?php _e( 'This is a meta box.', 'my-text-domain' ); ?></label>
<input type="text" name="my_meta_value" id="my_meta_value" value="<?php echo esc_attr( $value ); ?>" class="regular-text" />
</p>
<?php
}
/**
* Save the meta box.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*/
function prefix_meta_box_save( $post_id, $post ) {
// Check if our nonce is set.
if ( ! isset( $_POST['prefix_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['prefix_meta_box_nonce'], 'my_meta_box_nonce' ) ) {
return;
}
// Do not save for ajax requests.
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
return;
}
// Dont' save meta boxes for revisions or autosaves.
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
return;
}
// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
if ( empty( $_POST['post_ID'] ) || absint( $_POST['post_ID'] ) !== $post_id ) {
return;
}
// Check user has permission to edit.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Sanitize the user input.
$my_meta_value = sanitize_text_field( $_POST['my_meta_value'] );
// Update the meta field.
update_post_meta( $post_id, '_my_meta_value_key', $my_meta_value );
// Remove the save_post action.
remove_action( 'save_post', 'prefix_meta_box_save' );
}
add_action( 'save_post', 'prefix_meta_box_save', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment