我博客上的每个帖子都是一个事件,我需要为创建帖子时生成的每个事件创建一个新的邮件列表,以便用户可以订阅该事件的更新邮件列表。我非常接近但只需要一些指示。
目标:
问题:
transition_post_status
挂钩。)add_post_meta()
?答案 0 :(得分:0)
我的解决方案,使用DrewM's Mailchimp API wrapper:
<?php
use \DrewM\MailChimp\MailChimp;
function so_48291110_new_mailchimp_list_on_publish( $new_status, $old_status, $post ) {
// The first time our new post is published
if ( $new_status == 'publish' && $old_status != 'publish' ) {
// Mailchimp API instance
$MailChimp = new MailChimp('My Mailchimip API Key');
// Mailchimp Mailing List info
$mailchimp_new_list_data = array(
"name" => "Mailing list for {$post->post_title}",
"contact" => array(
"company" => "My company",
/* ... */
),
"campaign_defaults" => array(
"from_name" => "My name",
/* ... */
),
/* ... */
);
// Create new mailing list
$mailchimp_result = $MailChimp->post("lists", $mailchimp_new_list_data);
// Store mailing list ID as metadata
add_post_meta(
get_the_id(), // post ID
'mailchimp_list_id', // meta key
$mailchimp_result["id"], // meta value
true // unique
);
}
}
// Add action
add_action('transition_post_status', 'so_48291110_new_mailchimp_list_on_publish', 10, 3);