I'm using the following to send an email to the admin when a post is publish by an author:

add_action('publish_post', 'send_admin_email'); function send_admin_email($post_id){
    $to = 'admin@email.here';
    $subject = 'subject';
    $message = "Here is :".get_permalink($post_id);
    wp_mail($to, $subject, $message );

I would like to add the post author in $message.

I try get_the_author(); and the_author(); but nothing happens.


Missing global ?

global $post;

No global variable available if you are using publish_post action hook!

Ref : http://hungred.com/how-to/tutorial-post-id-publishpost-action-hook-wordpress/

Update 2 :

or try this

$author = get_userdata($post->post_author);

So you can use $author where you will :D

  • Like that ? : add_action('publish_post', 'send_admin_email'); function send_admin_email($post_id){global $post; $to = 'admin@email.here'; $subject = 'subject'; $message = "The author is :".get_the_author(); wp_mail($to, $subject, $message ); – Cat Oct 24 '12 at 13:32
  • I'm french so i don't understand your Ref very well. Sorry. :s – Cat Oct 24 '12 at 13:34
  • I forgot to say, my code work perfectly but i want to "add" the author in the mail. – Cat Oct 24 '12 at 13:49
  • Edited, check it out my answer @Cat – l2aelba Oct 24 '12 at 13:53
  • I try this add_action('publish_post', 'send_admin_email'); function send_admin_email($post_id){ $to = 'adress@email.fr'; $subject = "Un nouveau contrat SMP-PRO a été enregistré par".get_userdata($post->post_author); $message = "Sandra, le dossier ".get_the_title($post_id)." est visible sur cette page : ".get_edit_post_link( $id, $context ); wp_mail($to, $subject, $message ); } but it's not working… :( – Cat Oct 24 '12 at 14:04

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.