admin管理员组文章数量:1122832
i want to send email only new post publish in wordpress admin
when i pulish anything its sending mail to user how to restric this to only new post added to send mail to user
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
if ( $new_status !== 'publish' || $old_status === 'publish' )
return;
if ( ! $post_type = get_post_type_object( $post->post_type ) )
return;
global $wpdb;
$newsletterdata = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."newsletter");
foreach ( $newsletterdata as $newsletteremailall )
{
$newsletteremailall = $newsletteremailall->email;
if(filter_var($newsletteremailall, FILTER_VALIDATE_EMAIL))
{
$subject = $post->post_title;
$message = 'Our new post is here '."\n".'Post Title: ' .$post->post_title. "\n"."click here to visit post: " . get_permalink( $post->ID );
wp_mail( $newsletteremailall, $subject, $message );
}
else
{
}
}
}
add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
i want to send email only new post publish in wordpress admin
when i pulish anything its sending mail to user how to restric this to only new post added to send mail to user
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
if ( $new_status !== 'publish' || $old_status === 'publish' )
return;
if ( ! $post_type = get_post_type_object( $post->post_type ) )
return;
global $wpdb;
$newsletterdata = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."newsletter");
foreach ( $newsletterdata as $newsletteremailall )
{
$newsletteremailall = $newsletteremailall->email;
if(filter_var($newsletteremailall, FILTER_VALIDATE_EMAIL))
{
$subject = $post->post_title;
$message = 'Our new post is here '."\n".'Post Title: ' .$post->post_title. "\n"."click here to visit post: " . get_permalink( $post->ID );
wp_mail( $newsletteremailall, $subject, $message );
}
else
{
}
}
}
add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
Share
Improve this question
edited Jul 29, 2020 at 11:18
mozboz
2,6081 gold badge12 silver badges23 bronze badges
asked Jul 29, 2020 at 8:05
jykmhar123jykmhar123
1032 bronze badges
2 Answers
Reset to default 0Updated code for statuses as per @Fredrik's answer as his is more accurate.
To trigger this when publishing, you could change your first if to
if ( $new_status === 'inherit' && $old_status === 'new' )
That way, your code should trigger only when you publish a post.
EDIT:
Some notes on your code.
if ( ! $post_type = get_post_type_object( $post->post_type ) )
this will do nothing, as it's an assignment for a variable. It means, that you will get the current post type, and assign it to $post_type
and it will always pass.
What I think you're trying to achieve, is for this to work only for a specific post type. If that's the case, change this line to:
if ( 'my_custom_post_type' !== get_post_type_object( $post->post_type ) )
where my_custom_post_type
should be the post type for which you wish to send emails for.
For this line:
$newsletteremailall = $newsletteremailall->email;
It's a bad practice to overwrite the initial variable. Maybe name it $email
instead of $newsletteremailall
, because you may need it later, but you will no longer have access to it.
If you set the first if like this, it will ONLY send you these mails when you have created a NEW post. Not when you change it between different status. And this goes for all post types, you might (as mentioned before) look at the $post_type and change that to your custom post type, otherwise this is fired regardless of post type.
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
if ( $old_status === 'new' && $new_status === 'inherit' )
if ( ! $post_type = get_post_type_object( $post->post_type ) )
return;
global $wpdb;
$newsletterdata = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."newsletter");
foreach ( $newsletterdata as $newsletteremailall )
{
$newsletteremailall = $newsletteremailall->email;
if(filter_var($newsletteremailall, FILTER_VALIDATE_EMAIL))
{
$subject = $post->post_title;
$message = 'Our new post is here '."\n".'Post Title: ' .$post->post_title. "\n"."click here to visit post: " . get_permalink( $post->ID );
wp_mail( $newsletteremailall, $subject, $message );
}
else
{
}
}
}
add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
本文标签: actionsSend email to user when I publish a new post
版权声明:本文标题:actions - Send email to user when I publish a new post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299891a1930621.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论