我试图在自定义帖子类型首次发布时发送电子邮件,因为它将该帖子分配给一个人,并要求仅将电子邮件发送给该用户。用户是从列出特定用户角色的ACF选择框中选择的。
用户登录后,他们只能看到分配给他们的帖子,保存帖子后,他们可以填写其余自定义字段,然后向所有管理员发送电子邮件,说明用户已更新帖子。这是通过检查旧状态是否已发布和新状态是否已发布来实现的。
分配的用户只能在前端更新帖子,但管理员可以通过仪表板创建帖子,并可以在前端或仪表板中编辑帖子。
我的所有功能都正常工作,除了发送给ACF填写的post设置的自定义字段中的所选人员部分。在使用add\\u操作保存post之前,字段似乎不会被填充(“post\\u updated”、“updatepostsz”,10,3);这不允许我仅在更新已发布的帖子时使用ACF填充的信息。
如果我的代码看起来有误,那是因为我为了帮助解决这个问题而精简了代码以减少混乱。
我的代码是:
function updatepostsz( $post_id, $post_after, $post_before ) {
// Grab our post status
$post_status = get_post_status($post_id);
// get post type for checking
$post_type = get_post_type( $post_id );
// Before and after
$post_after_email_var = $post_after->post_status;
$post_before_email_var = $post_before->post_status;
// Stop anything from happening if revision or where we dont need it to be doing things
if ( wp_is_post_revision( $post_id ) ) { return; }
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;
// unhook this function so it doesn\'t loop infinitely
remove_action( \'save_post\', \'updateposts\' );
// If this isn\'t a custom post, run code.
if ( "task" != $post_type ) return;
// run codes on creation of new task only to email asigned person
if ( $post_before_email_var != \'publish\' && $post_after_email_var == \'publish\' )
{
// get user running script
$current_user = wp_get_current_user();
// the current users name
$cu = $current_user->user_login;
// all our information of custom post
$value_person = get_field( "responsible_person",$post_id );
// link to the post
$value_post_link = get_permalink( $post_id );
//grabs the id of the person assigned to
$select_staff_id = $value_personcu[\'ID\'];
// use our id to grab details on the user
$author_obj = get_user_by(\'id\', $select_staff_id);
$user_email = $author_obj->user_email;
//does not work in getting fields!!!!!!!!!!!!!!!
// Fields not saved until after this hook so email is empty
//////////////////////////////////////////////////////////////////
$to = $user_email;
$subject = \'A user email \'.$user_email.\' \'.$post_id.\' has been assigned to you\';
$body = \'You have been assigned a post task due by: \'.$value_date.\' \';
wp_mail( $to, $subject, $body);
///////////////////////////////////////////////////////////////////
}
// run codes only when a update is made to the task and works!
if ( $post_before_email_var == \'publish\' && $post_after_email_var == \'publish\' )
{
// get user running script
$current_user = wp_get_current_user();
// the current users name
$cu = $current_user->user_login;
// all our information of custom post
$value_person = get_field( "responsible_person" );
// link to the post
$value_post_link = get_permalink( $post_id );
//////////////////////////////////////////////////////////////////
$to = $user->user_email;
$subject = \'post updated by user\';
$body = \'body message\';
wp_mail( $to, $subject, $body);
///////////////////////////////////////////////////////////////////
}
}
add_action( \'post_updated\', \'updatepostsz\', 10, 3 );
感谢所有能够解决这个问题的人,因为我在谷歌上搜索并尝试了各种保存帖子的操作,但都没有成功。
SO网友:rudtek
为此,我使用了save\\u post\\u CPT()挂钩。
我不记得它是否第一次发送了内容:
请尝试以下代码:
function my_project_rt_send_email( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// If this is just a revision, don\'t send the email.
//if ( wp_is_post_revision( $post_id ) )
// return;
// only go if user checked media submission box.
if(isset($_POST[\'media-submit\'])){
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$post_content = get_the_content( $post_id );
$content_post = get_post( $post_id );
$content = $content_post->post_content;
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
$subject = \'Important - A NEW POST FOR YOU\';
$headers = array(\'Content-Type: text/html; charset=UTF-8\');
$headers[] = \'From: YOUR SITE <[email protected]>\';
// COPY USER BASED ON FIELD SEND TO USER BASED ON FIELD
if( get_field(\'distribution_email\') )
$headers[] = \'Bcc: \'. get_field(\'distribution_email\');
$message = "<h3>".$post_title."</h3>";
$message .= $content;
$message .= \'<a href="\'. $post_url.\'">\'.$post_title.\'</a>\';
// Send email to admin.
wp_mail( \'[email protected]\', $subject, $message, $headers);
}
}
add_action( \'save_post_my_cpt\', \'my_project_rt_send_email\' );
您需要将“save\\u post\\u MYCPT”更改为您的cpt名称,将第一个标头数组中的更改为电子邮件和发件人。
我不知道您的“收件人”字段是“选择”字段还是“文本”字段,在本例中,它是一个文本,因此您可能也需要更改它。
此外,我还有一个媒体复选框,所以也许可以删除
if(isset($_POST[\'media-submit\'])){
因为您需要它们,所以附件也会附带其附加的右括号“}”。