Create/Update a custom field when a post is published
-
Hello,
I have a plugin which creates automatically a custom field called ‘ytthumb_url’ when I publish a post from the backend of WordPress.
But when I publish content from frontend, via plugins like TDO Mini Forms, Post From Site, or others : the custom field is not created, I have to edit the post from the back-office to generate automatically the custom field.
Here is my plugin:
<?php add_action('save_post','find_ytid', 10, 2); function find_ytid($postID, $post) { if($parent_id = wp_is_post_revision($postID)) { $postID = $parent_id; } $content = $_POST['content']; if (preg_match('/http:\/\/www.youtube\.com\/v\/([a-zA-Z0-9\-\_]{11})/', $content, $yturl) !='') { $ytid = substr($yturl[0], 25, 31); $custom = 'https://img.youtube.com/vi/'.$ytid.'/hqdefault.jpg'; update_custom_meta($postID, $custom ,'ytthumb_url'); } } function update_custom_meta($postID, $newvalue, $field_name) { // To create new meta if(!get_post_meta($postID, $field_name)){ add_post_meta($postID, $field_name, $newvalue); }else{ // or to update existing meta update_post_meta($postID, $field_name, $newvalue); } } ?>
Anybody may help me ??
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Create/Update a custom field when a post is published’ is closed to new replies.