Do you want to automatically add custom fields when you publish your WordPress posts?
This is a simple trick that developers can use to enhance their WordPress website.
In this article, we’ll show you how to automatically add custom fields when you publish a post in WordPress.
Why Add Custom Fields Automatically?
Custom fields let you add extra information to your posts. This information can be shown on your website, kept private, or used by themes and plugins to make your WordPress site more powerful.
There are many ways to use custom fields. You can find some helpful ideas in our custom fields tips, tricks, and hacks guide.
Sometimes, you might want a custom field to be created automatically whenever you publish a post. This is especially useful when you’re adding new features to WordPress and using it for more than just blogging.
For example, we used this method to create a gallery website where we stored short URLs for each item. We automatically created a custom field to store the short URL whenever a post was published.
This trick is very useful for developers looking to extend WordPress functionality.
How to Automatically Add Custom Fields When Publishing Posts
This method involves adding a custom code snippet to your theme’s functions.php
file. We don’t recommend this for inexperienced users, because even a small mistake can break your website.
Instead, we’ll show you how to use the WPCode plugin in this tutorial.
WPCode makes it easy to add code snippets in WordPress without editing your theme’s functions.php
file. You can also manage all your code snippets from one central screen.
If this is your first time adding code to WordPress, check out our guide on how to copy and paste code snippets in WordPress for more details.
To get started, you need to install and activate the free WPCode plugin. If you need help, see our tutorial on how to install a WordPress plugin.
Note: The free version of WPCode has everything you need to add custom code in WordPress. For more advanced features like scheduled snippets, conversion pixels, and more, you can upgrade to WPCode Pro.
Once the plugin is activated, a new menu item labeled ‘Code Snippets’ will appear in your WordPress admin bar. Click on it and then press the ‘Add New’ button on the next screen.
Add Your Custom Code Snippet
From here, navigate to the ‘Add Your Custom Code (New Snippet)’ option and click on the ‘Use snippet’ button underneath it.
After that, give the snippet a title, and then copy the following code and paste it into the ‘Code Preview’ 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);
}
}
Replace ‘field-name’ and ‘custom value’ with the actual name and value you want to use for the custom field.
Choose ‘PHP Snippet’ as the code type from the dropdown menu on the right.
Next, scroll down to the ‘Insertion’ section. Here, you’ll need to leave the ‘Auto Insert’ method selected.
With the Auto Insert method, the snippet will be automatically inserted and executed in the proper location.
Once you’re done, toggle the switch from ‘Inactive’ to ‘Active’ and then click the ‘Save Snippet’ button.
Once the snippet is activated, the custom field will be created whenever you publish a post