Automatically add custom fields value to wordpress post

i’ve search on this site but apparentlly nothing is working to add custom field value when post publish , so i have custom fields name “quality” and i want to put value “HD” automatically when post publish.

so far i’ve tried this code :

/* Do something with the data entered */
add_action( ‘save_post’, ‘myplugin_save_postdata’ );
function myplugin_save_postdata( $post_id ) {
if ( ‘page’ == $_POST[‘post_type’] ) {
if ( ! current_user_can( ‘edit_page’, $post_id ) )
return;
} else {
if ( ! current_user_can( ‘edit_post’, $post_id ) )
return;
}

$mydata = ‘quality’; // Do something with $mydata

update_post_meta( $post_id, ‘HD’, $mydata );
}

and also this

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, ‘quality’, ‘HD’, true);
}
}

i put that on my functions.php and its not working , is there something wrong with what i do ?

Read more here:: Automatically add custom fields value to wordpress post

Leave a Reply

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