- BFA Collective
- Posts
- 1) Your WordPress post must actually contain an image in the content
1) Your WordPress post must actually contain an image in the content
Cool — Option B is the move. No flaky plugin, and it’ll run automatically for every new post (and you can bulk-run it too if you want).
The safest way to do Option B
Do NOT paste this into your theme files directly unless you’re using a child theme. Instead, use a reputable snippet manager:
WPCode (easy)
or Code Snippets
Either one is way safer than editing functions.php.
Step-by-step (WPCode / Code Snippets)
Install WPCode (or Code Snippets)
Create New Snippet → choose PHP Snippet
Paste the code below
Set it to Run everywhere (or “Frontend” is fine too)
Save and Activate
The code (Auto-set featured image from the first image in the post)
<?php
/**
* Auto-set featured image from the first image in the post content
* when a post is saved/updated and no featured image exists.
*/
add_action('save_post', function ($post_id) {
// Avoid autosaves, revisions, and non-post types if you want
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (wp_is_post_revision($post_id)) return;
// Only apply to standard posts (change/remove this if needed)
if (get_post_type($post_id) !== 'post') return;
// If it already has a featured image, do nothing
if (has_post_thumbnail($post_id)) return;
$post = get_post($post_id);
if (!$post || empty($post->post_content)) return;
// Find the first <img ... src="..."> in the content
if (!preg_match('/<img[^>]+src=["\']([^"\']+)["\']/i', $post->post_content, $matches)) {
return;
}
$image_url = $matches[1];
if (empty($image_url)) return;
// If the image is already in the Media Library, attach it
$attachment_id = attachment_url_to_postid($image_url);
// If not found in media library, sideload it (download + add to media library)
if (!$attachment_id) {
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_id = media_sideload_image($image_url, $post_id, null, 'id');
// media_sideload_image returns WP_Error on failure
if (is_wp_error($attachment_id)) return;
}
// Set featured image
set_post_thumbnail($post_id, (int) $attachment_id);
}, 20);
How to test (takes 60 seconds)
Create a test post that has an image in the body (at the top is best)
Do not set a featured image manually
Click Update/Publish
Refresh the editor → you should see the featured image filled in automatically
2 important notes (so it works every time)
1) Your WordPress post must actually contain an image in the content
If your Zap is only sending “preview text” (no <img>), there’s nothing to grab.
2) This can download external images into your Media Library
That’s good (stable thumbnails), but it means:
it creates a media file per post
make sure your image URLs are reachable publicly
If you want to avoid downloading and only use images already in Media Library
Tell me and I’ll tweak the snippet to only set featured images when the image is already uploaded.
Also: are your Beehiiv images coming into WordPress as a real <img> in the post body, or are you only seeing text?
Reply