0
votes

Whenever I post from the dashboard, I can grab the featured image URL with get_the_post_thumbnail_url() and display it in a email with wp_mail(). However, when I try this from the front-end, I get a empty URL.

Ive tried numerous frontend plugins but none works when it comes to the featured image. The rest of the post's fields display fine in the emails.

Im using the following code:

// POST MAILMAN

// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);

// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {


if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {

$latestPost_ID = $post->ID;
$latestPost_ImgUrl = get_the_post_thumbnail_url($latestPost_ID, 'full');
$latestPost_Category = get_the_category($latestPost_ID);
$latestPost_Category_Name = esc_html( $latestPost_Category[0]->name );
$latestPost_Url = get_post_permalink($latestPost_ID);
$latestPost_Title = get_post_field( 'post_title', $latestPost_ID );
$latestPost_Excerpt = get_post_field( 'post_excerpt', $latestPost_ID );
$todaydate = date("l") . ", " . date("j") . " " . date("F") . " " . date("Y");

to display the image in the email:

<td class="fluid-img" style="font-size:0pt; line-height:0pt; text-align:left;"><img src="' . esc_url($latestPost_ImgUrl) . '" border="0" width="650" height="366" alt="" /></td>

When done front-end vs backend: https://imgur.com/a/TqP9L6a

Some plugins I tried: https://wordpress.org/plugins/accesspress-anonymous-post/ https://wordpress.org/plugins/wp-user-frontend/

1
What is the logic here $recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );? $latestPost_ID it should be $latestPost_ID = $post->ID;Sky
Hi @Sky, I agree with you. I did that during testing. It basically grabs the the most recent posts, and limits it to 1. So its almost the same logic as $latestPost_ID = $post->ID; I replaced it now and everything still acts the same when testingMaartenPAC

1 Answers

-1
votes

I find getting the Featured Image via it's attachment record is more reliable, for either public-facing or admin requests. Try this:

$attachment = wp_get_attachment_image_src(get_post_thumbnail_id($latestPost_ID), 'full', true);
$featuredImageUrl = $attachment[0];