How to Add Custom Field Dynamically to WordPress Post/Page when Published

x theme
(Last Updated On: July 10, 2014)

Here, we’ll show a method of adding custom field dynamically when you’re publishing any post or page. You’ll put the code in functions.php. But you’ll have to specify the field name and custom value later.

Things you’ll have to change in the code

  • FIELD_NAME (FIELD_NAME could be anything like: buy, timing etc. Whatever you want)
  • CUSTOM VALUE (Custom value could be anything, you want to put and display)

How to Add Custom Field Dynamically to Post/Page when Published

function add_custom_field_dynamically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'FIELD_NAME', 'CUSTOM VALUE', true);
add_action('publish_page', 'add_custom_field_dynamically');
add_action('publish_post'. 'add_custom_field_dynamically');

About the author


Leave a Reply

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