I am trying to send an email after a new post has been created. It needs to be sent after the creation of the post, because I want to include the post category in the email. wp_after_insert_post
does not seem to work, as no email is sent. I have tried using the same code with the publish_post
hook instead, which does send an email correctly, but I am then unable to use the category name in the email.
function new_post_email( $post_id ) {
$categories = get_the_category( $post_id );
$subject = $categories[0]->name;
$sent = wp_mail($to = '[email protected]', $subject, $message = 'Test message');
};
add_action( ' wp_after_insert_post', 'new_post_email');
Is there any solution for this? Thanks in advance
It's possible your
wp_mail()
function syntax is incorrect, you also have an extra space in youradd_action()
. I've not seen documentation or examples where you can assign variables a value in the function itself. I would try either declaring your variables outside the function, like so:Or just put the values directly in that don't have declared vars for: