0

我正在尝试在创建新帖子后发送电子邮件。它需要在创建帖子后发送,因为我想在电子邮件中包含帖子类别。wp_after_insert_post似乎不起作用,因为没有发送电子邮件。我尝试使用与publish_post钩子相同的代码,它确实可以正确发送电子邮件,但我无法在电子邮件中使用类别名称。

function new_post_email( $post_id ) {        
    $categories = get_the_category( $post_id );
    $subject = $categories[0]->name;

    $sent = wp_mail($to = 'my@email.com', $subject, $message = 'Test message');
};
add_action( ' wp_after_insert_post', 'new_post_email');

有什么解决办法吗?提前致谢

4

1 回答 1

0

您的wp_mail()函数语法可能不正确,您的add_action(). 我还没有看到可以在函数本身中为变量赋值的文档或示例。我会尝试在函数之外声明变量,如下所示:

function new_post_email( $post_id ) {        
    $categories = get_the_category( $post_id );
    $subject = $categories[0]->name;
    $to = 'myemail.com';
    $message = 'Test message';

    $sent = wp_mail($to, $subject, $message);

};
add_action( 'wp_after_insert_post', 'new_post_email'); // remove space in wp_after_insert...

或者只是将值直接放入没有声明变量的值中:

function new_post_email( $post_id ) {        
    $categories = get_the_category( $post_id );
    $subject = $categories[0]->name;

    $sent = wp_mail('my@email.com', $subject, 'Test message');
};
add_action( 'wp_after_insert_post', 'new_post_email'); // remove space in wp_after_insert...
于 2022-01-27T14:22:15.507 回答