<input type="email" name="subscriber_email" ...">

WordPress订阅功能全面实现指南

一、邮件订阅系统实现

1. 基础邮件订阅表单

// 创建订阅表单短代码
function subscribe_form_shortcode() {
ob_start(); ?>
<form id="email-subscribe" method="post">
<input type="email" name="subscriber_email" required placeholder="输入您的邮箱地址">
<button type="submit">订阅</button>
</form>
<?php
return ob_get_clean();
}
add_shortcode('subscribe_form', 'subscribe_form_shortcode');

// 处理订阅请求
add_action('admin_post_nopriv_subscribe_email', 'handle_subscription');
add_action('admin_post_subscribe_email', 'handle_subscription');

function handle_subscription() {
if (!isset($_POST['subscriber_email']) || !is_email($_POST['subscriber_email'])) {
wp_die('无效的邮箱地址');
}

$email = sanitize_email($_POST['subscriber_email']);
$subscribers = get_option('site_subscribers', []);

if (!in_array($email, $subscribers)) {
$subscribers[] = $email;
update_option('site_subscribers', $subscribers);

// 发送确认邮件
wp_mail($email, '订阅确认', '感谢订阅我们的内容');
}

wp_redirect($_SERVER['HTTP_REFERER']);
exit;
}

2. 集成Mailchimp

// Mailchimp API集成
function mailchimp_subscribe($email) {
$api_key = get_option('mailchimp_api_key');
$list_id = get_option('mailchimp_list_id');

$data_center = substr($api_key, strpos($api_key, '-')+1);
$url = "https://{$data_center}.api.mailchimp.com/3.0/lists/{$list_id}/members";

$response = wp_remote_post($url, [
'headers' => [
'Authorization' => 'Basic ' . base64_encode('user:'. $api_key)
],
'body' => json_encode([
'email_address' => $email,
'status' => 'subscribed'
])
]);

return !is_wp_error($response);
}

二、RSS订阅增强功能

1. 自定义RSS源

// 创建自定义RSS订阅源
function custom_rss_feed() {
add_feed('custom', 'custom_rss_template');
}
add_action('init', 'custom_rss_feed');

function custom_rss_template() {
get_template_part('rss', 'custom');
exit;
}

2. RSS订阅按钮

function rss_subscribe_button() {
return '<a href="'.get_bloginfo('rss2_url').'" class="rss-subscribe">订阅RSS</a>';
}
add_shortcode('rss_button', 'rss_subscribe_button');

三、内容推送订阅

1. 新内容发布通知

// 文章发布时通知订阅者
add_action('publish_post', 'notify_subscribers', 10, 2);

function notify_subscribers($ID, $post) {
$subscribers = get_option('site_subscribers', []);
$subject = '新内容发布: ' . get_the_title($ID);
$message = '查看最新内容: ' . get_permalink($ID);

foreach ($subscribers as $email) {
wp_mail($email, $subject, $message);
}
}

2. 定时摘要邮件

// 每周摘要邮件
add_action('weekly_digest', 'send_weekly_digest');

function send_weekly_digest() {
$subscribers = get_option('site_subscribers', []);
$posts = get_posts([
'date_query' => [
'after' => '1 week ago'
],
'posts_per_page' => 5
]);

$subject = '本周内容摘要';
$message = "本周热门内容:\n\n";

foreach ($posts as $post) {
$message .= get_the_title($post->ID) . "\n";
$message .= get_permalink($post->ID) . "\n\n";
}

foreach ($subscribers as $email) {
wp_mail($email, $subject, $message);
}
}

// 设置定时任务
if (!wp_next_scheduled('weekly_digest')) {
wp_schedule_event(time(), 'weekly', 'weekly_digest');
}

四、会员订阅系统

1. 订阅等级设置

// 创建订阅等级
function create_subscription_levels() {
$levels = [
'basic' => [
'name' => '基础会员',
'price' => 9.99,
'features' => ['每周摘要', '基础内容']
],
'premium' => [
'name' => '高级会员',
'price' => 29.99,
'features' => ['所有内容', '专属邮件', '提前访问']
]
];

update_option('subscription_levels', $levels);
}

2. 订阅支付处理

// 处理订阅支付
function process_subscription_payment($user_id, $level) {
$levels = get_option('subscription_levels');

if (!isset($levels[$level])) {
return false;
}

// 记录订阅信息
update_user_meta($user_id, 'subscription_level', $level);
update_user_meta($user_id, 'subscription_expiry', strtotime('+1 month'));

return true;
}

五、订阅管理后台

1. 订阅者列表管理

// 添加订阅管理菜单
add_action('admin_menu', 'add_subscribers_menu');

function add_subscribers_menu() {
add_users_page(
'订阅者管理',
'订阅者',
'manage_options',
'subscribers',
'render_subscribers_page'
);
}

function render_subscribers_page() {
$subscribers = get_option('site_subscribers', []);
?>
<div class="wrap">
<h1>订阅者管理</h1>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>邮箱地址</th>
<th>订阅时间</th>
</tr>
</thead>
<tbody>
<?php foreach ($subscribers as $email) : ?>
<tr>
<td><?php echo esc_html($email); ?></td>
<td><?php echo get_option('subscriber_time_' . md5($email)); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}

2. 订阅数据导出

// 导出订阅者数据
add_action('admin_init', 'export_subscribers');

function export_subscribers() {
if (!isset($_GET['export_subscribers'])) {
return;
}

$subscribers = get_option('site_subscribers', []);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="subscribers.csv"');

$output = fopen('php://output', 'w');
fputcsv($output, ['Email']);

foreach ($subscribers as $email) {
fputcsv($output, [$email]);
}

fclose($output);
exit;
}

六、订阅功能优化

1. 双重确认订阅

// 发送确认链接
function send_confirmation_email($email) {
$token = wp_generate_password(32, false);
set_transient('sub_confirm_' . $token, $email, WEEK_IN_SECONDS);

$confirm_url = add_query_arg([
'action' => 'confirm_subscription',
'token' => $token
], home_url());

$message = "请点击以下链接确认订阅:\n\n{$confirm_url}";
wp_mail($email, '请确认您的订阅', $message);
}

// 处理确认请求
add_action('init', 'handle_confirmation');

function handle_confirmation() {
if (!isset($_GET['action']) || $_GET['action'] !== 'confirm_subscription') {
return;
}

$token = sanitize_text_field($_GET['token']);
$email = get_transient('sub_confirm_' . $token);

if ($email) {
$subscribers = get_option('site_subscribers', []);
if (!in_array($email, $subscribers)) {
$subscribers[] = $email;
update_option('site_subscribers', $subscribers);
update_option('subscriber_time_' . md5($email), current_time('mysql'));
}
delete_transient('sub_confirm_' . $token);
wp_redirect(home_url('/subscription-confirmed'));
exit;
}
}

2. 订阅偏好设置

// 添加快捷取消订阅链接
function add_unsubscribe_link($email) {
$token = wp_generate_password(32, false);
set_transient('unsub_token_' . md5($email), $token, MONTH_IN_SECONDS);

$unsub_url = add_query_arg([
'action' => 'unsubscribe',
'email' => urlencode($email),
'token' => $token
], home_url());

return "\n\n取消订阅: {$unsub_url}";
}

// 处理取消订阅请求
add_action('init', 'handle_unsubscribe');

function handle_unsubscribe() {
if (!isset($_GET['action']) || $_GET['action'] !== 'unsubscribe') {
return;
}

$email = sanitize_email(urldecode($_GET['email']));
$token = sanitize_text_field($_GET['token']);
$stored_token = get_transient('unsub_token_' . md5($email));

if ($token === $stored_token) {
$subscribers = get_option('site_subscribers', []);
$key = array_search($email, $subscribers);

if ($key !== false) {
unset($subscribers[$key]);
update_option('site_subscribers', $subscribers);
delete_transient('unsub_token_' . md5($email));
wp_redirect(home_url('/unsubscribe-success'));
exit;
}
}
}

通过以上方法,可以在WordPress主题中实现完整的订阅功能体系,包括邮件订阅、内容推送、会员订阅等多种形式。这些功能可以根据实际需求进行组合和扩展,构建适合自己网站的订阅系统。

我爱主题网 自2012
主题:260+ 销售:1000+
兼容浏览器

电话咨询

7*12服务咨询电话:

1855-626-3292

微信咨询