Last updated on September 25, 2023

Add a Custom Field to a Post or Page

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Extend posts with custom fields.

To add a custom field to a post or page in WordPress, you can use the add_meta_box() function. This function allows you to create a custom field box that will appear on the post or page editor screen.

Here’s an example of how to add a custom field to a post:

function wpsnippets_add_custom_field() {
    add_meta_box(
        'custom_field_id', // Unique ID for the meta box
        'Custom Field', // Title of the meta box
        'wpsnippets_render_custom_field', // Callback function to render the content of the meta box
        'post', // Post type(s) to display the meta box
        'normal', // Context where the meta box should be displayed
        'high' // Priority of the meta box
    );
}
add_action('add_meta_boxes', 'wpsnippets_add_custom_field');

function wpsnippets_render_custom_field($post) {
    $value = get_post_meta($post->ID, 'custom_field_key', true);
    ?>
    <label for="custom_field">Custom Field:</label>
    <input type="text" id="custom_field" name="custom_field" value="<?php echo esc_attr($value); ?>">
    <?php
}

function wpsnippets_save_custom_field($post_id) {
    if (isset($_POST['custom_field'])) {
        update_post_meta($post_id, 'custom_field_key', sanitize_text_field($_POST['custom_field']));
    }
}
add_action('save_post', 'wpsnippets_save_custom_field');

In this example, we first define the wpsnippets_add_custom_field() function which uses the add_meta_box() function to create a custom field box. We specify the unique ID, title, callback function to render the content, post type(s) to display the meta box, and the context and priority of the meta box.

Next, we define the wpsnippets_render_custom_field() function which is the callback function used to render the content of the custom field box. Inside this function, we retrieve the current value of the custom field using the get_post_meta() function and display an input field with the value.

Finally, we define the wpsnippets_save_custom_field() function which is hooked to the save_post action. This function checks if the custom field value is set in the $_POST data and updates the post meta using the update_post_meta() function.

You can modify this code to suit your needs by changing the custom field ID, title, and key, as well as the input field type and name.

Examples

Example 1: Adding a Custom Field to a Post

This use case demonstrates how to add a custom field to a post in WordPress. The code example uses the add_meta_box() function to create a custom meta box, and the save_post action hook to save the custom field value.

function wpsnippets_add_custom_field() {
    add_meta_box(
        'custom_field',
        'Custom Field',
        'wpsnippets_render_custom_field',
        'post',
        'normal',
        'default'
    );
}
add_action('add_meta_boxes', 'wpsnippets_add_custom_field');

function wpsnippets_render_custom_field($post) {
    $value = get_post_meta($post->ID, 'custom_field', true);
    ?>
    <label for="custom_field">Custom Field:</label>
    <input type="text" id="custom_field" name="custom_field" value="<?php echo esc_attr($value); ?>">
    <?php
}

function wpsnippets_save_custom_field($post_id) {
    if (isset($_POST['custom_field'])) {
        update_post_meta($post_id, 'custom_field', sanitize_text_field($_POST['custom_field']));
    }
}
add_action('save_post', 'wpsnippets_save_custom_field');

The add_meta_box() function is used to create a custom meta box with the title “Custom Field” on the post edit screen. The wpsnippets_render_custom_field() function is responsible for rendering the HTML input field for the custom field. The save_post action hook is used to save the custom field value when the post is saved.

Example 2: Adding a Custom Field to a Page

This use case demonstrates how to add a custom field to a page in WordPress. The code example is similar to Example 1, but with a different post type specified in the add_meta_box() function.

function wpsnippets_add_custom_field() {
    add_meta_box(
        'custom_field',
        'Custom Field',
        'wpsnippets_render_custom_field',
        'page',
        'normal',
        'default'
    );
}
add_action('add_meta_boxes', 'wpsnippets_add_custom_field');

function wpsnippets_render_custom_field($post) {
    $value = get_post_meta($post->ID, 'custom_field', true);
    ?>
    <label for="custom_field">Custom Field:</label>
    <input type="text" id="custom_field" name="custom_field" value="<?php echo esc_attr($value); ?>">
    <?php
}

function wpsnippets_save_custom_field($post_id) {
    if (isset($_POST['custom_field'])) {
        update_post_meta($post_id, 'custom_field', sanitize_text_field($_POST['custom_field']));
    }
}
add_action('save_post', 'wpsnippets_save_custom_field');

The code example is similar to Example 1, but with the post type set to 'page' in the add_meta_box() function. This will add the custom field to the page edit screen instead of the post edit screen.

Example 3: Adding Multiple Custom Fields to a Post

This use case demonstrates how to add multiple custom fields to a post in WordPress. The code example extends Example 1 by adding additional custom fields to the meta box.

function wpsnippets_add_custom_field() {
    add_meta_box(
        'custom_fields',
        'Custom Fields',
        'wpsnippets_render_custom_fields',
        'post',
        'normal',
        'default'
    );
}
add_action('add_meta_boxes', 'wpsnippets_add_custom_field');

function wpsnippets_render_custom_fields($post) {
    $fields = array(
        'custom_field_1' => 'Custom Field 1',
        'custom_field_2' => 'Custom Field 2',
        'custom_field_3' => 'Custom Field 3',
    );

    foreach ($fields as $key => $label) {
        $value = get_post_meta($post->ID, $key, true);
        ?>
        <label for="<?php echo $key; ?>"><?php echo $label; ?>:</label>
        <input type="text" id="<?php echo $key; ?>" name="<?php echo $key; ?>" value="<?php echo esc_attr($value); ?>">
        <?php
    }
}

function wpsnippets_save_custom_fields($post_id) {
    $fields = array(
        'custom_field_1',
        'custom_field_2',
        'custom_field_3',
    );

    foreach ($fields as $field) {
        if (isset($_POST[$field])) {
            update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
        }
    }
}
add_action('save_post', 'wpsnippets_save_custom_fields');

The wpsnippets_render_custom_fields() function is responsible for rendering multiple custom fields within the meta box. The custom fields are defined in the $fields array, where the keys represent the meta keys and the values represent the field labels. The wpsnippets_save_custom_fields() function is responsible for saving the values of the custom fields.

Last updated on September 25, 2023. Originally posted on October 13, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *