在 WordPress 中发布帖子时向用户发送电子邮件

Sis*_*sir 1 php wordpress wordpress-theming

我有一个前端发布表单,注册用户可以在其中起草帖子。我有一个元字段,其中包含起草内容的用户的电子邮件。现在,当我从后端发布内容时,我想向用户发送一封电子邮件,通知该帖子已发布。

任何类型的研究方向都会有所帮助。

谢谢!

Kis*_*han 5

add_action( 'publish_post', 'send_notification' );
function send_notification( $post_id ) {    
        $post     = get_post($post_id);
        $post_url = get_permalink( $post_id );
        $post_title = get_the_title( $post_id ); 
        $author   = get_userdata($post->post_author);
        $subject  = 'Post publish notification';
        $message  = "Hello,";
        $message .= "<a href='". $post_url. "'>'".$post_title."'</a>\n\n";
        wp_mail($author->user_email, $subject, $message );  
}
Run Code Online (Sandbox Code Playgroud)