Ultimate Guide to Custom Meta Boxes in WordPress

In WordPress, `add_meta_boxes` is an action hook that allows developers to add custom meta boxes to different post types’ edit screens. Meta boxes are additional sections on the post editor screen where you can add and display custom fields or other types of data related to the post.

Here’s how you can use `add_meta_boxes` to add a custom meta box to a post type (e.g., “post” or “page”):

 

// Add the custom meta box
function my_custom_meta_box() {
    add_meta_box(
        'my_custom_meta_box_id',     // Unique ID of the meta box
        'My Custom Meta Box',        // Title of the meta box
        'display_custom_meta_box',   // Callback function to display the content of the meta box
        'post',                      // Post type where the meta box should be displayed
        'normal',                    // Context (normal, advanced, or side)
        'high'                       // Priority (high, default, low, or core)
    );
}
add_action( 'add_meta_boxes', 'my_custom_meta_box' );

// Display the content of the custom meta box
function display_custom_meta_box( $post ) {
    // Retrieve any existing values for the custom fields
    $custom_field_value = get_post_meta( $post->ID, '_custom_field_key', true );

    // Nonce field to verify the data when submitting
    wp_nonce_field( 'my_custom_meta_box_nonce_action', 'my_custom_meta_box_nonce' );

    // Output the HTML for your custom meta box content
    ?>
    <label for="custom_field">Custom Field:</label>
    <input type="text" id="custom_field" name="custom_field" value="<?php echo esc_attr( $custom_field_value ); ?>" style="width: 100%;">
    <?php
}

// Save the custom meta box data when the post is saved or updated
function save_custom_meta_box_data( $post_id ) {
    // Verify the nonce
    if ( ! isset( $_POST['my_custom_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['my_custom_meta_box_nonce'], 'my_custom_meta_box_nonce_action' ) ) {
        return;
    }

    // Check if this is an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    // Save the custom field value
    if ( isset( $_POST['custom_field'] ) ) {
        update_post_meta( $post_id, '_custom_field_key', sanitize_text_field( $_POST['custom_field'] ) );
    }
}
add_action( 'save_post', 'save_custom_meta_box_data' );

 

This code will add a custom meta box to the post editor screen in the WordPress admin for the “post” post type. The meta box contains a single text input field where you can enter and save custom data.

Make sure to replace `’my_custom_meta_box_id’` and `’my_custom_meta_box_nonce_action’` with unique IDs for your meta box and nonce action to avoid conflicts with other plugins or themes.

Remember to adjust the post type and customize the HTML inside the `display_custom_meta_box` function according to your specific requirements.

Categories

Related Blogs

Ajax Add to Cart Quantity on Shop WooCommerce

To enable AJAX add to cart with quantity selectors on the WooCommerce shop page, you’ll need to use JavaScript to handle the AJAX request and update the cart quantity dynamically. Below are the steps to achieve this:

Disable plugin update in WordPress

While it’s generally not recommended to disable plugin updates in WordPress due to security and functionality improvements, there might be specific cases where you need to prevent certain plugins from updating. Keep in mind that doing so may leave your site vulnerable to security issues and may cause compatibility problems with future WordPress versions.

Customize Woocommerce Settings Tab

Let’s talk about customizing Woocommerce Admin setting Tabs.To hide a specific Woocommerce setting tab or want to customize the Woocommerce setting tabs, not the entire sub-menu, but just a tab.For just Removing tabs we can use