1

I have a frontend form that uses wp_insert_post() to programmatically insert posts into the site with "pending" status. I'm also using insert_attachment() to attach the submitted file to the post.

Now I'm trying to write code to automatically email the post content (including attachments) to the site administrator. Here is the code that I'm using to do that in the functions.php file:

// Notify admin on post insert (pending)
add_action('wp_insert_post', 'send_email_on_pending_post_creation' );
function send_email_on_pending_post_creation( $post_id){
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$post_content = get_post_field('post_content', $post_id);
$subject = 'New post pending!';

$message = "A new post is pending on the website:";
$message .= "<br><br>";
$message .= "<strong>Post Title: </strong>";
$message .= "<a href='" . $post_url . "'>" .$post_title. "</a>";
$message .= "<br><br>";
$message .= "<strong>Post Content:</strong><br>";
$message .= $post_content;
$message .= "<br><br>";

//$attachments = get_attached_media( 'image' );

$attachments = get_the_post_thumbnail( $post_id, 'full' );

$msg_headers = array('Content-Type: text/html; charset=UTF-8', 'From: "Website" < no-reply@website.com >');

//send email to admin
wp_mail( 'admin@website.com', $subject, $message, $msg_headers, $attachments );
}

Now the problem is that although the email is being sent with the post title and the content, the attachment doesn't get sent. Also, doing a var_dump($attachments) once the email is sent ends up being empty.

Please help me figure out what I'm doing wrong and what needs to be done.

Thank you!

EDIT: Finally found the solution. Please check my answer bellow in case you come across this problem.

  • Please show us where you are setting the post thumbnail here. – GhostToast Sep 26 '14 at 12:46
  • @GhostToast: Just updated the question with the code that attaches the post thumbnail. Thanks! – Avinash Kumar Sep 26 '14 at 13:14
  • Can anybody help me on this? Thanks! – Avinash Kumar Sep 27 '14 at 3:15
1

After asking this question in several places and not getting a solution, I found the solution this morning myself.

Instead of hooking the function send_email_on_pending_post_creation to the "wp_insert_post" action, you need to hook it to the "add_attachment" action which occurs AFTER the post is inserted and when the attachment is added to the post. You also need to update the code in the send_email_on_pending_post_creation function to fetch the post_parent (the parent post ID to which the attachment is attached) so that the post_title, post_url and post_content variables return the correct values.

Here is the updated send_email_on_pending_post_creation function which sends an email to the admin with an attachment link.

add_action('add_attachment', 'send_email_on_pending_post_creation' );

function send_email_on_pending_post_creation( $post_id ){
$attachment = get_post($post_id);
$attachment_title = get_the_title($post_id);

$parent_id = $attachment->post_parent;

$post_title = get_the_title( $parent_id );
$post_url = get_permalink( $parent_id );
$post_content = get_post_field('post_content', $parent_id);

$subject = 'New post pending!';

$attachments = wp_get_attachment_url($post_id);

$message = "A new post is pending on the website:";
$message .= "<br><br>";
$message .= "<strong>Post Title: </strong>";
$message .= "<a href='" . $post_url . "'>" .$post_title. "</a>";
$message .= "<br><br>";
$message .= "<strong>Post Content:</strong><br>";
$message .= $post_content;
$message .= "<br><br>";
$message .= "<strong>Attachment:</strong><br><br>";
$message .= "<a href='". $attachments . "' title='". $attachment_title . "'>" . $attachment_title . "</a>";
$message .= "<br><br>";

$msg_headers = array('Content-Type: text/html; charset=UTF-8', 'From: "Website" < no-reply@website.com >');

//send email to admin
wp_mail( 'admin@website.com', $subject, $message, $msg_headers, $attachments );
}

Hope it helps somebody!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.