WordPress 自动随机重新发布旧文章

这个插件是使用 ChatGPT 写的,说实话对于不大懂代码的人来说 Ai 确实帮了大忙。

具体功能如下

  • 随机更新已经发布过的旧文章
  • 可以自行设定发布的时间间隔
  • 可以自行设定发布的文章类型
  • 可以查看已经重新发布了哪些文章
  • 可以查看下一篇即将发布的文章

不过也因为是 ChatGPT 写的所以多多少存在一些 Bug

  • 在修改发布时间后需要点击两次保存按钮
  • 激活插件时会自动更新一篇文章
  • 点击保存按钮后会自动更新一篇文章
  • 插件为英文插件但是也很容易看懂

下面是添加插件的方法,或者也可以下载插件后安装

首先,创建一个新的文件夹,命名为 auto-publish-pending-posts,在该文件夹中创建一个名为 auto-publish-pending-posts.php 的 PHP 文件,这将是插件的主文件。在 auto-publish-pending-posts.php 文件中添加以下内容:

<?php
/*
Plugin Name: Random Update Old Posts
Description: Automatically updates a random old post to the current time every hour and logs the updates.
Version: 1.5
Author: emodelai
*/

// Hook into the activation to schedule the event
register_activation_hook(__FILE__, 'ruop_schedule_event');
function ruop_schedule_event() {
    if (!wp_next_scheduled('ruop_update_random_post')) {
        $interval = get_option('ruop_update_interval', 'hourly');
        wp_schedule_event(time(), $interval, 'ruop_update_random_post');
    }
    ruop_set_next_post_to_update();
}

// Hook into the deactivation to clear the scheduled event
register_deactivation_hook(__FILE__, 'ruop_clear_scheduled_event');
function ruop_clear_scheduled_event() {
    $timestamp = wp_next_scheduled('ruop_update_random_post');
    if ($timestamp) {
        wp_unschedule_event($timestamp, 'ruop_update_random_post');
    }
}

// Add custom intervals
add_filter('cron_schedules', 'ruop_custom_cron_schedule');
function ruop_custom_cron_schedule($schedules) {
    for ($i = 1; $i <= 24; $i++) {
        $schedules["{$i}_hours"] = array(
            'interval' => $i * 60 * 60,
            'display'  => sprintf(__('Every %d Hours'), $i)
        );
    }
    return $schedules;
}

// Hook the function to the scheduled event
add_action('ruop_update_random_post', 'ruop_update_random_post');
function ruop_update_random_post() {
    // Get the next post to update
    $next_post_id = get_option('ruop_next_post_to_update');

    if ($next_post_id) {
        // Update the post date to the current time
        $update_post = array(
            'ID'            => $next_post_id,
            'post_date'     => current_time('mysql'),
            'post_date_gmt' => current_time('mysql', 1),
            'post_modified'     => current_time('mysql'),
            'post_modified_gmt' => current_time('mysql', 1),
        );
        wp_update_post($update_post);

        // Log the update
        $log = get_option('ruop_update_log', array());
        $log[] = array(
            'post_id' => $next_post_id,
            'time' => current_time('mysql')
        );
        update_option('ruop_update_log', $log);
    }

    // Set the next post to update
    ruop_set_next_post_to_update();
}

// Set the next post to update
function ruop_set_next_post_to_update() {
    // Fetch a random post
    $post_type = get_option('ruop_post_type', 'post');
    $args = array(
        'post_type'      => $post_type,
        'posts_per_page' => 1,
        'orderby'        => 'rand'
    );
    $random_post = get_posts($args);

    if (!empty($random_post)) {
        $next_post_id = $random_post[0]->ID;
        update_option('ruop_next_post_to_update', $next_post_id);
    }
}

// Add an admin menu item
add_action('admin_menu', 'ruop_add_admin_menu');
function ruop_add_admin_menu() {
    add_menu_page(
        'Update Log',
        'Update Log',
        'manage_options',
        'ruop-update-log',
        'ruop_display_update_log',
        'dashicons-update',
        20
    );
}

function ruop_display_update_log() {
    // Handle log clear action
    if (isset($_POST['ruop_clear_log'])) {
        check_admin_referer('ruop_clear_log_action', 'ruop_clear_log_nonce');
        delete_option('ruop_update_log');
        echo '<div class="updated"><p>' . __('Log cleared.', 'textdomain') . '</p></div>';
    }

    // Get the update log
    $log = get_option('ruop_update_log', array());

    // Sort the log by time in descending order
    usort($log, function($a, $b) {
        return strtotime($b['time']) - strtotime($a['time']);
    });

    // Pagination parameters
    $per_page = 10;
    $current_page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
    $total_pages = ceil(count($log) / $per_page);
    $offset = ($current_page - 1) * $per_page;

    // Slice the log for pagination
    $log_page = array_slice($log, $offset, $per_page);

    // Get the next scheduled event
    $timestamp = wp_next_scheduled('ruop_update_random_post');
    $next_update = $timestamp ? date('Y-m-d H:i:s', $timestamp) : __('Not scheduled', 'textdomain');

    // Get the next post to update
    $next_post_id = get_option('ruop_next_post_to_update');
    $next_post_title = $next_post_id ? get_the_title($next_post_id) : __('No post selected', 'textdomain');

    // Get the selected update interval and post type
    $update_interval = get_option('ruop_update_interval', 'hourly');
    $post_type = get_option('ruop_post_type', 'post');

    // Display the log, next update time, next post to be updated, update interval, and post type
    ?>
    <div class="wrap">
        <h1><?php _e('Update Log', 'textdomain'); ?></h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('ruop_settings_group');
            do_settings_sections('ruop-update-log');
            ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row"><?php _e('Update Interval', 'textdomain'); ?></th>
                    <td>
                        <select name="ruop_update_interval">
                            <?php for ($i = 1; $i <= 24; $i++) : ?>
                                <option value="<?php echo esc_attr("{$i}_hours"); ?>" <?php selected(get_option('ruop_update_interval', 'hourly'), "{$i}_hours"); ?>>
                                    <?php echo esc_html("Every {$i} Hours"); ?>
                                </option>
                            <?php endfor; ?>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row"><?php _e('Post Type', 'textdomain'); ?></th>
                    <td>
                        <select name="ruop_post_type">
                            <?php
                            $post_types = get_post_types(array('public' => true), 'objects');
                            foreach ($post_types as $post_type_obj) : ?>
                                <option value="<?php echo esc_attr($post_type_obj->name); ?>" <?php selected(get_option('ruop_post_type', 'post'), $post_type_obj->name); ?>>
                                    <?php echo esc_html($post_type_obj->labels->singular_name); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
        <p><?php _e('Next update:', 'textdomain'); ?> <?php echo $next_update; ?></p>
        <p><?php _e('Next post to be updated:', 'textdomain'); ?> <?php echo esc_html($next_post_title); ?> (ID: <?php echo esc_html($next_post_id); ?>)</p>
        <p><?php _e('Update Interval:', 'textdomain'); ?> <?php echo esc_html($update_interval); ?></p>
        <p><?php _e('Post Type:', 'textdomain'); ?> <?php echo esc_html($post_type); ?></p>
        <table class="widefat fixed" cellspacing="0">
            <thead>
                <tr>
                    <th><?php _e('Post ID', 'textdomain'); ?></th>
                    <th><?php _e('Title', 'textdomain'); ?></th>
                    <th><?php _e('Update Time', 'textdomain'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($log_page)) : ?>
                    <tr>
                        <td colspan="3"><?php _e('No updates yet.', 'textdomain'); ?></td>
                    </tr>
                <?php else : ?>
                    <?php foreach ($log_page as $entry) : ?>
                        <tr>
                            <td><?php echo esc_html($entry['post_id']); ?></td>
                            <td><?php echo esc_html(get_the_title($entry['post_id'])); ?></td>
                            <td><?php echo esc_html($entry['time']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
        <?php
        // Display pagination
        if ($total_pages > 1) {
            $current_url = menu_page_url('ruop-update-log', false);
            echo '<div class="tablenav"><div class="tablenav-pages">';
            echo paginate_links(array(
                'base' => add_query_arg('paged', '%#%', $current_url),
                'format' => '',
                'prev_text' => __('&laquo;'),
                'next_text' => __('&raquo;'),
                'total' => $total_pages,
                'current' => $current_page
            ));
            echo '</div></div>';
        }
        ?>
        <form method="post">
            <?php wp_nonce_field('ruop_clear_log_action', 'ruop_clear_log_nonce'); ?>
            <input type="hidden" name="ruop_clear_log" value="1">
            <?php submit_button(__('Clear Log', 'textdomain'), 'delete'); ?>
        </form>
    </div>
    <?php
}

// Register settings
add_action('admin_init', 'ruop_register_settings');
function ruop_register_settings() {
    register_setting('ruop_settings_group', 'ruop_update_interval');
    register_setting('ruop_settings_group', 'ruop_post_type');
}

// Update scheduled event on interval change
add_action('update_option_ruop_update_interval', 'ruop_reschedule_event');
add_action('update_option_ruop_post_type', 'ruop_reschedule_event');
function ruop_reschedule_event() {
    ruop_clear_scheduled_event();
    ruop_schedule_event();
}
?>

激活插件后在 Update Log 就能看到插件的具体内容了

资源下载
下载地址1立即下载
常见问题
123盘资源下载
本站提供 123云盘 资源链接
可无登入直接下载
安全声明
如文章内提供下载内容
此内容可能为执行脚本,软件,图像或Ai模型
所有内容均经过病毒查杀,可放心下载
免责声明
因模型可能包含 NSFW 内容,请不要将模型用于非法用途
本站点只提供模型下载,不参与制作者图片生成
因制作者生成图片造成的违法问题与本站无关
0

评论0

没有账号?注册  忘记密码?