Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Add Custom Fields Automatically on Post Publish in WordPress

Do you need to automatically create custom fields when publishing your WordPress posts?

This is a simple trick that developers can use when adding new features to their WordPress website.

In this article, we’ll show you how to add custom fields automatically on post publish in WordPress.

How to Add Custom Fields Automatically on Post Publish in WordPress

Why Add Custom Fields Automatically?

Custom fields let you add additional information to your posts. This information can be displayed on your website, kept private, or be used by themes and plugins to extend the functionality of your WordPress website.

There are many ways to use custom fields. You’ll find a list of helpful ideas in our custom fields tips, tricks, and hacks guide.

Sometimes you’ll want a custom field to be created automatically whenever you publish a post. This is especially true when you’re adding functionality to WordPress so you can use it as more than a simple blog.

We used this method when creating a gallery website. We wanted to store short URLs for each item submitted to the gallery. So we automatically created a custom field to store the short URL when each post was published.

This trick can be very useful for developers who are looking to push WordPress to the next level.

Adding Custom Fields Automatically on Post Publish

In this tutorial, we’ll be adding a custom code snippet to your theme’s functions.php file. We don’t recommend this method to inexperienced users, because even a small mistake could break your website.

If this is your first time adding code to your WordPress files, then you should check out our guide on how to copy and paste code snippets in WordPress. In this article, we’ll show you how to use the Code Snippets plugin.

Once you’ve installed and activated the Code Snippets plugin, navigate to the Snippets section of your WordPress dashboard. Once there, you’ll need to click the Add New button.

Click the Add New Button to Add a Custom Code Snippet

You need to give the snippet a title, and then you should copy the following code and paste it into the code box.

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', 'custom value', true);
    }
}

You’ll have to replace ‘field-name’ and ‘custom value’ with the actual name and value you want to use for the custom field.

Paste the Code Snippet in the Code Box

Once you’re done, you’ll need to click the Save Changes and Activate button at the bottom of the screen. Once the snippet is activated, the custom field will be created whenever you publish a post.

Click the Save Changes and Activate Button

We hope this tutorial helped you learn how to add custom fields automatically on post publish in WordPress.

You may also want to learn how to choose the best WordPress hosting, or check out our list of must have WordPress plugins to grow your website.

If you liked this guide, then please consider subscribing to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

16 CommentsLeave a Reply

  1. I want to add category Id for the post in the post_meta table. How can be the function function add_custom_field_automatically($post_ID) be twicked to accomodate that?

    Thanks

  2. Instead of adding the custom field at the time of creating the post, how do I display a custom field by default on the admin page?

    • If you notice, the code above doesn’t have anything related to the custom post types. It only adds to Post and Page “content type”. So you would have to specify the hook for your custom post type.

      Admin

      • Thanks for the reply. Can you help me out on this code.
        add_action(‘publish_page’, ‘add_custom_field_automatically’);
        add_action(‘publish_post’, ‘add_custom_field_automatically’);
        add_action( ‘save_post’, ‘add_custom_field_automatically’ );

        function add_custom_field_automatically($post_ID) {
        global $post;
        if(!wp_is_post_revision($post_ID)) {
        global $wpdb;
        $user_id = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $post_ID");
        $themename = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘themeperauthor’ AND user_id = $user_id");
        add_post_meta($post_ID, ‘themeperauthor’, $themename, true);
        }
        }
        When the user meta field is changed, I want to automatically delete the custom field value and update with the new one

  3. I’ve been working on adding some hidden custom fields on post publish/update (by preceding the field name with “_”) but for some reason I have to update the post twice before the wp_postmeta entries are written to the database.

    I’ve tried messing with the priority and experimenting with other action hooks (draft_to_publish, edit_post, etc) but it doesn’t seem to make a difference.

    Any ideas?

  4. will this work too for custom post types?
    sth like add_action(‘publish_custom-post-typ-name’, ‘add_custom_field_automatically’);

    • Yes, it will.

      One downside to this technique that people should be aware of is that if somebody edits a published post, this hook WILL get fired again on the edit. Therefore, you need to check for the meta before adding it, or to update it, or to do whatever makes the most sense for your use-case.

      If you only want to get your code fired off on the initial publish only, then you can use the transition_post_status hook. This hook works like this:


      add_action('transition_post_status','example',10,3);
      function example($new, $old, $post) {
      // $new is the new post status ('publish')
      // $old is the old post status ('draft')
      // $post is the complete Post Object (so use $post->ID for the ID, etc)
      }

      Then, in here you can do a check for something like this:

      if ($new == 'publish' && $old != 'publish')

      To have your code only used when the post status actually transitions to publish from whatever it was before. This hook is fired at the same time as the {$status}_{$post-type} hooks are, so the operation of them is basically the same.

  5. thanks for this handy tip. previously while using custom fields i have to work manually. now this will do it automatically. thanks for saving precious time of me

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.