Adding A Custom Field Automatically On Post/Page Publish
When managing a WordPress site, you might want to add a custom field automatically whenever a post or page is published. This can help automate metadata insertion, improve content organization, or trigger custom workflows without manual input. This tutorial shows you how to add a custom field on publish in WordPress using modern, best-practice code that works with both classic and block editors.
When to Use This
- Automatically tagging posts with metadata like source, author notes, or custom flags.
- Adding default values to custom fields for SEO, analytics, or content management.
- Triggering custom plugin or theme logic that depends on post meta.
- Ensuring consistency across published content without relying on manual input.
Updated Code for Modern WordPress
WordPress provides hooks like save_post and transition_post_status to detect when a post is published. The recommended approach is to use save_post combined with checks for the post status to ensure the custom field is added only once on publish. This avoids duplicate or unnecessary updates.
Here’s a clean, modern example that adds a custom field named my_custom_field with the value published_value when a post or page is published:
function add_custom_field_on_publish( $post_id, $post, $update ) {
    // Avoid recursion and autosaves
    if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
        return;
    }
    // Only proceed if post status is 'publish'
    if ( $post->post_status !== 'publish' ) {
        return;
    }
    // Check if the custom field already exists to avoid overwriting
    if ( get_post_meta( $post_id, 'my_custom_field', true ) ) {
        return;
    }
    // Add the custom field
    add_post_meta( $post_id, 'my_custom_field', 'published_value', true );
}
add_action( 'save_post', 'add_custom_field_on_publish', 10, 3 );How to Add This Code
You can add this code either directly to your theme’s functions.php file or create a small custom plugin. Both methods are straightforward:
Option 1: Add to functions.php
- Access your WordPress site files via FTP or hosting file manager.
- Navigate to wp-content/themes/your-active-theme/.
- Open functions.phpin a code editor.
- Paste the code snippet at the end of the file.
- Save and upload the file back.
Option 2: Create a Small Plugin
- Create a new folder named auto-custom-fieldinsidewp-content/plugins/.
- Create a file named auto-custom-field.phpinside that folder.
- Paste the following code inside auto-custom-field.php:
<?php
/*
Plugin Name: Auto Custom Field on Publish
Description: Adds a custom field automatically when a post or page is published.
Version: 1.0
Author: Your Name
*/
function add_custom_field_on_publish( $post_id, $post, $update ) {
    if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
        return;
    }
    if ( $post->post_status !== 'publish' ) {
        return;
    }
    if ( get_post_meta( $post_id, 'my_custom_field', true ) ) {
        return;
    }
    add_post_meta( $post_id, 'my_custom_field', 'published_value', true );
}
add_action( 'save_post', 'add_custom_field_on_publish', 10, 3 );- Save the file.
- Go to WordPress admin > Plugins and activate Auto Custom Field on Publish.
Step-by-Step Test
- Add the code via functions.phpor activate the plugin.
- Create a new post or page in WordPress admin.
- Publish the post/page.
- Go to the post editor, open the “Custom Fields” panel (enable it via Screen Options if hidden).
- Verify that the custom field my_custom_fieldexists with the valuepublished_value.
- Try updating the post; the custom field should not be overwritten or duplicated.
Block Themes & Gutenberg Notes
- This method works regardless of whether you use the classic editor or Gutenberg block editor.
- Block themes do not affect how post meta is saved; the save_posthook fires normally.
- If you want to expose this custom field in the block editor UI, consider registering it with register_post_meta()for REST API support.
- For example, to register the meta for Gutenberg:
function register_my_custom_meta() {
    register_post_meta( '', 'my_custom_field', [
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string',
        'auth_callback' => function() {
            return current_user_can( 'edit_posts' );
        },
    ]);
}
add_action( 'init', 'register_my_custom_meta' );Common Pitfalls
- Autosave and revisions: Always check for autosaves and revisions to prevent unintended meta updates.
- Duplicate meta: Use get_post_meta()to check if the field already exists before adding.
- Post status checks: Ensure the post is actually published before adding the field.
- Cache issues: If you use object caching, clear caches to see changes immediately.
- Custom post types: Modify the code if you want to target custom post types by checking $post->post_type.
Works on
- Web servers: Apache, Nginx, LiteSpeed
- Control panels: cPanel, Plesk, DirectAdmin
- WordPress versions 5.0 and above (supports Gutenberg and classic editor)
