Family Encyclopedia >> Electronics

How to add custom fields automatically in post post in WordPress

When creating our WPBeginner Gallery site, we needed to automatically generate custom short URLs for each site submitted to the gallery, and then store them as a custom field as soon as the post was published. Now, while we leave the automatically generated shortlinks for each post to our next topic, we will cover how to automatically add custom fields on the post post in WordPress. This can be very useful for developers looking to push WordPress to the next level.

The first thing you need to do is open the functions.php topic. file and paste the following code:

add_action ('publish_page', 'add_custom_field_automatically'); add_action ('publish_post', 'add_custom_field_automatically'); función add_custom_field_automatically ($ post_ID) global $ wpdb; if (! wp_is_post_revision ($ post_ID)) add_post_meta ($ post_ID, 'nombre de campo', 'valor personalizado', verdadero); 

Then simply replace the custom field name and value with your custom Field Name and Value. This is a relatively simple trick, but it can be very effective when trying to use WordPress for purposes other than blogging.