我现在使用的是“The Events Calendar“我的网站上的插件,用于协调员工团队。我希望能够自动发送包含活动帖子正文的电子邮件(帖子类型不是$post
, 但是$tribe_events
相反)每次都会更新,但仅针对该职位相关的员工。
为了获取电子邮件地址,我在$tribe_events
根据我在网上找到的添加社交媒体按钮的插件发布type editor,效果很好:
add_action( \'add_meta_boxes\', \'add_custom_box\' );
function add_custom_box( $tribe_events ) {
add_meta_box(
\'animateurs\', // ID, should be a string.
\'Animateurs\', // Meta Box Title.
\'social_services\', // Your call back function, this is where your form field will go.
\'tribe_events\', // The post type you want this to show up on, can be post, page, or custom post type.
\'side\', // The placement of your meta box, can be normal or side.
\'core\' // The priority in which this will be displayed.
);
}
function social_services( $tribe_events )
{
// Get post meta value using the key from our save function in the second paramater.
$custom = get_post_meta($tribe_events->ID, \'_social_services\', true);
?>
<input type="checkbox" id="anim_patrick" name="social_services[]" value="[email protected]" <?php echo (in_array(\'[email protected]\', $custom)) ? \'checked="checked"\' : \'\'; ?>>
<label for="anim_patrick"></label>Patrick<br>
<input type="checkbox" id="anim_jeff" name="social_services[]" value="[email protected]" <?php echo (in_array(\'[email protected]\', $custom)) ? \'checked="checked"\' : \'\'; ?>>
<label for="anim_jeff"></label>Jeff<br>
<?php
}
function save_extra_fields(){
global $post;
if(isset( $_POST[\'social_services\'] ))
{
$custom = $_POST[\'social_services\'];
$old_meta = get_post_meta($post->ID, \'_social_services\', true);
// Update post meta
if(!empty($old_meta)){
update_post_meta($post->ID, \'_social_services\', $custom);
} else {
add_post_meta($post->ID, \'_social_services\', $custom, true);
}
}
// update_post_meta($post->ID, "producers", $_POST["producers"]);
}
add_action( \'save_post\', \'save_extra_fields\' );
The result
这将成功保存活动帖子所涉及的员工的电子邮件地址。
我可以使用此代码显示这些电子邮件地址的数组(在页面上-不是我想做的。这纯粹是为了显示如何从数组中获取电子邮件地址):
<?php $meta = get_post_meta($post->ID, \'_social_services\', true); $anims = implode (", ", $meta); echo $anims; ?>
我需要的是最后一部分,那就是向中包含的电子邮件地址发送一封包含最新帖子内容的电子邮件
$anims
每次
$tribe_events
职位类型已更新。
我如何才能做到这一点?
PS:我是PHP的nube。这是我第一次尝试这样的事情。
最合适的回答,由SO网友:Mohammad Mursaleen 整理而成
你就快到了。你只需要使用wp_mail 发送数组中包含的电子邮件。
下面是如何使用save_post 行动挂钩。
/**
* To send email on updating your event post
*
* @param int $post_id The post ID.
* @param post $post The post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function send_email_on_event_update( $post_id, $post, $update ) {
$post_type = \'tribe_events\'; // your post type
// If this isn\'t a \'tribe_events\' post, No need to send emails
if ( $post_type != $post->post_type ) {
return;
}
// - Now to Send emails
// first get array of your emails
$all_emails = get_post_meta($post_id , \'_social_services\', true); // this is suppose to be variable containing array of emails
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = $post_title;
$message = "This event has been updated:\\n\\n";
$message .= $post_title . ": " . $post_url;
$message .= "keep adding what ever you want to send in this email...";
// Send email to $all_emails.
wp_mail( $all_emails, $subject, $message );
}
// have kept the priority to 11 to make sure we get the content for email after every thing is saved
add_action( \'save_post\', \'send_email_on_event_update\', 11, 3 );
尚未测试此代码。但我相信这对你有用。