Add Custom Fields to WordPress Posts Using a Code Snippet

Marek T Jan 3, 2025 Custom Fields
How can I add extra information to my blog posts that aren’t in the usual places?
What is the process for adding custom fields to WordPress posts programmatically via a PHP code snippet?
Andy answered Jan 3, 2025

Adding Custom Fields to WordPress Posts Programmatically

Method 1: Register Custom Meta Fields

This code adds a custom meta box to the post editor and saves the custom field data:

Code to add custom meta box and save functionality (place in your theme's functions.php or in a custom plugin):

// Add meta box to posts
function add_custom_meta_box() {
    add_meta_box(
        'custom_post_meta_box',
        'Additional Information',
        'display_custom_meta_box',
        'post',
        'normal',
        'high'
    );
}
add_action('add_meta_boxes', 'add_custom_meta_box');

// Display meta box content
function display_custom_meta_box($post) {
    wp_nonce_field('custom_meta_box_nonce', 'custom_meta_box_nonce');
    $value = get_post_meta($post->ID, '_custom_field_key', true);
    ?>
    <label for="custom_field">Your Custom Field:</label>
    <input type="text" id="custom_field" name="custom_field" value="<?php echo esc_attr($value); ?>" size="50" />
    <?php
}

// Save meta box data
function save_custom_meta_box($post_id) {
    if (!isset($_POST['custom_meta_box_nonce']) || 
        !wp_verify_nonce($_POST['custom_meta_box_nonce'], 'custom_meta_box_nonce')) {
        return;
    }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    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');

Method 2: Display Custom Field in Posts

Add this code to display the custom field value in your posts:

function display_custom_field_content($content) {
    if (is_single() && get_post_type() === 'post') {
        $custom_field = get_post_meta(get_the_ID(), '_custom_field_key', true);
        if (!empty($custom_field)) {
            $content .= '<div class="custom-field-content">' . esc_html($custom_field) . '</div>';
        }
    }
    return $content;
}
add_filter('the_content', 'display_custom_field_content');

Plugin Alternative

If you prefer using a plugin, here are reliable options:

  1. Advanced Custom Fields (ACF) - Most popular solution with a user-friendly interface
  2. Custom Field Suite - Lightweight alternative to ACF
  3. Pods - Full content management framework

Usage Notes

  • The code creates a text input field, but you can modify it to support other input types (textarea, select, etc.)
  • Change _custom_field_key to your desired meta key name
  • Adjust the meta box title 'Additional Information' to your needs
  • Modify the display function to change how the custom field appears on your posts
  • The code includes proper security checks and data sanitization

The custom field will appear in the post editor below the content area, and its value will display at the bottom of your post content on the frontend.