RSS/Feed 订阅延迟发布

有些人会通过 RSS/Feed 订阅采集你的站点内容,甚至比你更早被搜索引擎收录,所以才有了这个插件。本插件为 WordPress RSS Feed 添加延迟发布、特色图、版权信息和“查看全文”链接。支持后台自定义设置。

倘若你觉得自己的网站收录时间较长,可适当增加延迟发布的时间。

插件下载安装

<?php
/*
Plugin Name: WPEXP - 订阅延迟发布
Plugin URI: https://wpexp.cn/plugins/dingyueyanchi
Description: 为 WordPress RSS Feed 添加延迟发布、特色图、版权信息和“查看全文”链接。支持后台自定义设置。
Version: 1.1
Author: WPEXP
Author URI: https://wpexp.cn/
License: GPL2
*/

if (!defined('ABSPATH')) {
    exit; // 禁止直接访问
}

define('WPEXP_DINGYUEYANCHI_OPTIONS', 'wpexp_dingyueyanchi_options');

// 默认设置
function wpexp_dingyueyanchi_get_default_options() {
    return array(
        'delay_hours' => 6,
        'show_full_content' => false,
        'custom_copyright' => " &raquo; 转载请保留版权:<a title=\"缙哥哥的博客\" href=\"https://www.dujin.org/\">缙哥哥的博客</a> &raquo; <a rel=\"bookmark\" title=\"[文章标题]\" href=\"[文章链接]\">《[文章标题]》</a></div>\n &raquo; 本文链接地址:<a rel=\"bookmark\" title=\"[文章标题]\" href=\"[文章链接]\">[文章链接]</a>\n &raquo; 如果喜欢可以关注微信公众号:dujinorg",
    );
}

// 注册设置页面
function wpexp_dingyueyanchi_menu() {
    add_options_page(
        '订阅延迟发布设置',
        '订阅延迟发布',
        'manage_options',
        'wpexp-dingyueyanchi',
        'wpexp_dingyueyanchi_settings_page'
    );
}
add_action('admin_menu', 'wpexp_dingyueyanchi_menu');

// 注册设置
function wpexp_dingyueyanchi_register_settings() {
    register_setting(WPEXP_DINGYUEYANCHI_OPTIONS, WPEXP_DINGYUEYANCHI_OPTIONS, 'wpexp_dingyueyanchi_sanitize');
}
add_action('admin_init', 'wpexp_dingyueyanchi_register_settings');

// 设置表单
function wpexp_dingyueyanchi_settings_page() {
    $options = get_option(WPEXP_DINGYUEYANCHI_OPTIONS, wpexp_dingyueyanchi_get_default_options());
    ?>
    <div class="wrap">
        <h1>订阅延迟发布设置</h1>
        <form method="post" action="options.php">
            <?php settings_fields(WPEXP_DINGYUEYANCHI_OPTIONS); ?>
            <?php do_settings_sections(WPEXP_DINGYUEYANCHI_OPTIONS); ?>

            <table class="form-table">
                <tr valign="top">
                    <th scope="row">延迟发布时间(小时)</th>
                    <td><input type="number" name="<?php echo WPEXP_DINGYUEYANCHI_OPTIONS; ?>[delay_hours]" value="<?php echo esc_attr($options['delay_hours']); ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">是否显示全文</th>
                    <td>
                        <label><input type="checkbox" name="<?php echo WPEXP_DINGYUEYANCHI_OPTIONS; ?>[show_full_content]" value="1" <?php checked(1, $options['show_full_content']); ?> /> 勾选后 RSS Feed 显示全文内容</label>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">自定义版权信息</th>
                    <td>
                        <textarea name="<?php echo WPEXP_DINGYUEYANCHI_OPTIONS; ?>[custom_copyright]" rows="8" cols="80" class="large-text code"><?php echo esc_textarea($options['custom_copyright']); ?></textarea>
                        <p class="description">支持变量:[文章标题]、[文章链接]</p>
                    </td>
                </tr>
            </table>

            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

// 数据清洗
function wpexp_dingyueyanchi_sanitize($input) {
    $output = wpexp_dingyueyanchi_get_default_options();

    $output['delay_hours'] = absint($input['delay_hours']);
    $output['show_full_content'] = isset($input['show_full_content']) ? 1 : 0;
    $output['custom_copyright'] = wp_kses_post($input['custom_copyright']);

    return $output;
}

// 获取设置
function wpexp_dingyueyanchi_get_options() {
    return get_option(WPEXP_DINGYUEYANCHI_OPTIONS, wpexp_dingyueyanchi_get_default_options());
}

// RSS Feed 延迟发布
function wpexp_publish_later_on_feed($where) {
    $options = wpexp_dingyueyanchi_get_options();
    if (is_feed()) {
        global $wpdb;
        $now = gmdate('Y-m-d H:i:s');
        $wait = $options['delay_hours'];
        $where .= " AND TIMESTAMPDIFF(HOUR, $wpdb->posts.post_date_gmt, '$now') > $wait ";
    }
    return $where;
}
add_filter('posts_where', 'wpexp_publish_later_on_feed');

// RSS Feed 内容控制(是否显示全文)
function wpexp_feed_content_control($content) {
    $options = wpexp_dingyueyanchi_get_options();

    if (is_feed()) {
        if ($options['show_full_content']) {
            remove_all_filters('the_excerpt_rss');
            return get_the_content();
        } else {
            return $content;
        }
    }
    return $content;
}
add_filter('the_excerpt_rss', 'wpexp_feed_content_control', 999);
add_filter('the_content_feed', 'wpexp_feed_content_control', 999);

// RSS Feed 添加“查看全文”链接(仅在摘要模式下)
function wpexp_feed_read_more($content) {
    $options = wpexp_dingyueyanchi_get_options();
    if (is_feed() && !$options['show_full_content']) {
        $content .= '<p><a rel="bookmark" href="' . get_permalink() . '" target="_blank">查看全文</a></p>';
    }
    return $content;
}
add_filter('the_excerpt_rss', 'wpexp_feed_read_more');

// RSS Feed 添加文章特色图像(缩略图)
function wpexp_rss_post_thumbnail($content) {
    global $post;
    if (is_feed()) {
        if (has_post_thumbnail($post->ID)) {
            $output = get_the_post_thumbnail($post->ID);
            $content = $output . $content;
        }
    }
    return $content;
}
add_filter('the_excerpt_rss', 'wpexp_rss_post_thumbnail');
add_filter('the_content_feed', 'wpexp_rss_post_thumbnail');

// RSS Feed 添加版权信息
function wpexp_feed_copyright($content) {
    if (is_feed()) {
        $options = wpexp_dingyueyanchi_get_options();
        $copyright = str_replace(
            array('[文章标题]', '[文章链接]'),
            array(get_the_title(), get_permalink()),
            $options['custom_copyright']
        );
        $content .= "<blockquote>" . $copyright . "</blockquote>";
    }
    return $content;
}
add_filter('the_content', 'wpexp_feed_copyright');

发表评论