Reputation: 2173
I was trying to add featured image from post content. My idea is to set the first image of the content as featured image of the post.
I wrote this in my funtions.php
add_action('publish_post', 'auto_featured_image_publish_post');
function auto_featured_image_publish_post($post, $post_id) {
$thumnail_id = //something I need help with
set_post_thumbnail( $post, $thumbnail_id );
}
This is just an idea how I want to do this.
Please help. Thanks
Upvotes: 2
Views: 1700
Reputation: 909
Check this url
OR you can use this code to get the url of the first image from any HTML code
<?php
$postcontent = "";
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $postcontent, $image);
// you can use the exist pattern or use this
// '/< *img[^>]*src *= *["\']?([^"\']*)/i'
echo $image['src'];
?>
Upvotes: 1