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 = '[email protected]';
    $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.


1 Answer 1


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 = '[email protected]'; $subject = 'subject'; $message = "The author is :".get_the_author(); wp_mail($to, $subject, $message );
    – Cat
    Commented Oct 24, 2012 at 13:32
  • I'm french so i don't understand your Ref very well. Sorry. :s
    – Cat
    Commented Oct 24, 2012 at 13:34
  • I forgot to say, my code work perfectly but i want to "add" the author in the mail.
    – Cat
    Commented Oct 24, 2012 at 13:49
  • Edited, check it out my answer @Cat
    – l2aelba
    Commented Oct 24, 2012 at 13:53
  • I try this add_action('publish_post', 'send_admin_email'); function send_admin_email($post_id){ $to = '[email protected]'; $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
    Commented Oct 24, 2012 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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