Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Adding A Custom Field Automatically On Post/Page Publish

Posted on August 19, 2025 By Admin No Comments on Adding A Custom Field Automatically On Post/Page Publish

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

  1. Access your WordPress site files via FTP or hosting file manager.
  2. Navigate to wp-content/themes/your-active-theme/.
  3. Open functions.php in a code editor.
  4. Paste the code snippet at the end of the file.
  5. Save and upload the file back.

Option 2: Create a Small Plugin

  1. Create a new folder named auto-custom-field inside wp-content/plugins/.
  2. Create a file named auto-custom-field.php inside that folder.
  3. 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 );
  1. Save the file.
  2. Go to WordPress admin > Plugins and activate Auto Custom Field on Publish.

Step-by-Step Test

  1. Add the code via functions.php or activate the plugin.
  2. Create a new post or page in WordPress admin.
  3. Publish the post/page.
  4. Go to the post editor, open the “Custom Fields” panel (enable it via Screen Options if hidden).
  5. Verify that the custom field my_custom_field exists with the value published_value.
  6. 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_post hook 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)
WordPress Snippets Tags:Custom Fields, Meta, PHP, WordPress

Post navigation

Previous Post: Showing Random Posts In WordPress
Next Post: Get Separate Count For Comments, Trackbacks, And Pingbacks In WordPress

Leave a Reply Cancel reply

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

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com