wordpress定时自动发布文章插件

wordpress定时自动发布文章插件-源码网
wordpress定时自动发布文章插件
此内容为付费阅读,请付费后查看
29.9
立即购买
您当前未登录!建议登陆后购买,可保存购买订单
付费阅读

写一个wordpress定时自动发布文章插件,设置要求:自动发布基础间隔10分钟,随机浮动间隔1-120秒,每天最多发布文章数50篇,可发布时间段8:00-18:00,需要发布的分类,可发布文章类型为文章,文章数据源:等待审核的文章,发布模式:随机选择一篇文章发布,提供代码form.php

<?php 
if ( ! defined( 'ABSPATH' ) ) exit; 

if($_POST['update_draft']){
	if ( ! isset( $_POST['name_of_nonce_field'] ) 
    	|| ! wp_verify_nonce( $_POST['name_of_nonce_field'], 'name_of_my_action' ) 
	) {
   		print 'Sorry, your nonce did not verify.';
	} else {
		update_option('WIND_start_date',sanitize_text_field($_POST['start_date']));
		update_option('WIND_start_time',sanitize_text_field($_POST['start_time']));
		update_option('WIND_interval_time',sanitize_text_field($_POST['interval_to_publish']));
		update_option('WIND_draft_num',sanitize_text_field($_POST['publish_num']));
		update_option('WIND_draf_orderby',sanitize_text_field($_POST['orderby']));
		update_option('WIND_recurrence',sanitize_text_field($_POST['recurrence']));
		update_option('WIND_recurrence_times',sanitize_text_field($_POST['recurrence_to_publish']));
	
		$starTime=get_option('WIND_start_time');
		wp_schedule_event( strtotime(get_option('WIND_start_date')." ".$starTime),'WIND_custom_recurrence','WIND_cron_draft_update_hook' );
	}
}
if($_POST['delete_draft']){
	if ( ! isset( $_POST['name_of_nonce_field'] ) 
    	|| ! wp_verify_nonce( $_POST['name_of_nonce_field'], 'name_of_my_action' ) 
	) {

   		print 'Sorry, your nonce did not verify.';
	} else {
		wp_clear_scheduled_hook( 'WIND_cron_draft_update_hook' );
	}
}
?>

<style type="text/css">
#main{ width:700px; border:1px solid #ccc; background-color:#f9f9f9; padding:10px; margin-top:20px; }
.button-primary{margin-right:20px;}
</style>

<div class="wrap">

	<div id="icon-options-general" class="icon32"><br></div><h2><?php  _e('Schedule Task', 'wp-auto-publish'); ?></h2>
	
	<div class="card">
	
		<h3><?php  _e('Auto Publish Setting', 'wp-auto-publish'); ?></h3>
		<hr>
		<div id="draft">
		<form action="" method="post">
			<p><label for="start_publish"><?php  _e('Begin From:', 'wp-auto-publish'); ?></label>
			<input type="date" id="start_date" name="start_date" value="<?php echo get_option('WIND_start_date'); ?>" />
			<?php  _e('Time:', 'wp-auto-publish'); ?><input style="width:100px;" type="time" id="start_time" name="start_time" value="<?php echo get_option('WIND_start_time'); ?>" /> 
			<?php  _e('Time Format', 'wp-auto-publish'); ?> 11:35</p>
			<p><label for="interval_to_publish"><?php  _e('Interval', 'wp-auto-publish'); ?></label>
			<input style="width:60px;" type="number" min="1" id="interval_to_publish" name="interval_to_publish" value="<?php echo get_option('WIND_interval_time'); ?>" />
			<label for="publish_num"><?php  _e('second,', 'wp-auto-publish'); _e('Total of publishing', 'wp-auto-publish'); ?></label>
			<input style="width:60px;" type="number" min="1" id="publish_num" name="publish_num" value="<?php echo get_option('WIND_draft_num'); ?>" />
			<label for="orderby"><?php  _e('Post,', 'wp-auto-publish');_e('Orderby', 'wp-auto-publish'); ?></label>
			<select name="orderby">
				<option value="rand" <?php selected( get_option('WIND_draf_orderby'), 'rand' ); ?>><?php  _e('Random', 'wp-auto-publish'); ?></option>
				<option value="ID" <?php selected( get_option('WIND_draf_orderby'), 'ID' ); ?>>ID</option>
			</select>
			</p>
			<p>
			<label for="recurrence"><?php  _e('Repeat by', 'wp-auto-publish'); ?></label><input style="width:60px;" type="number" min="0" id="recurrence_to_publish" name="recurrence_to_publish" value="<?php echo get_option('WIND_recurrence_times'); ?>" />
			<select name="recurrence">
				<option value="daily" <?php selected( get_option('WIND_recurrence'), 'daily' ); ?>><?php  _e('day', 'wp-auto-publish'); ?></option>
				<option value="hourly" <?php selected( get_option('WIND_recurrence'), 'hourly' ); ?>><?php  _e('hour', 'wp-auto-publish'); ?></option>
				<option value="weekly" <?php selected( get_option('WIND_recurrence'), 'weekly' ); ?>><?php  _e('week', 'wp-auto-publish'); ?></option>
			</select> <?php  _e('input 0,no-repeat', 'wp-auto-publish'); ?>
			</P>
			<p>
		<?php 
		submit_button( __('Update and Start', 'wp-auto-publish'),'primary','update_draft',''); 
		echo"  " ;
		submit_button(__('Cancel the Scheduled Task', 'wp-auto-publish'),'secondary','delete_draft','');?>
			</p>
			<?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>
		</form>
		<?php if(wp_next_scheduled('WIND_cron_draft_update_hook')):?><p style="color:red"><?php  _e('Next scheduled task time:', 'wp-auto-publish'); ?>
			<?php echo date('Y-m-d H:i:s',wp_next_scheduled('WIND_cron_draft_update_hook'));?></p>
		<?php else:?>
		<p style="color:gray;"><?php  _e('The scheduled task has stopped!', 'wp-auto-publish'); ?></p>
		<?php endif;?>
		
		</div>
		<hr>
		<div>
			<h3><?php  _e('Instructions for use', 'wp-auto-publish'); ?></h3>
			<?php  _e('Save all the articles you plan to publish as a draft and make the settings above, then <strong> Update and Start </strong> .', 'wp-auto-publish'); ?>
		</div>
	</div>
<div  class="card">
	<p>技术支持qq 8013006</p>
</div>
</div>

这个插件将每10分钟检查一次,并在8:00至18:00之间发布最多50篇待审文章。文章发布会随机选择文章分类(你需要替换 ‘your_category_slug’ 为你的分类的 slug),并随机选择一篇文章进行发布。wp-auto-publish.php

Automatic batch timing post. 灵活设定自动批量定时发布文章。

== Description ==
Save all the posts that you plan to publish as a draft and make some settings, then Update and Start.
将所有要计划发布的文章存为草稿,并进行按提示设定,然后 更新执行计划 系统即可按计划运行;
1、可以指定计划任务的开始时间,发布时间间隔,发布数量,
2、可选择按ID升序或随机发布草稿文章。
3、可设置任务的重复间隔(天,小时或周)

== Installation ==
1. WP-Auto-Publish folder to the plugins directory in your WordPress installation.
2. Activate the plugin.
3. Navigate to the “Auto Publish” Menu.
1. 将下载的文件解压缩,然后将`wp-auto-publish`文件夹 上传到 `/wp-content/plugins/`目录
2.在插件后台启用
3.然后在后台管理菜单,点选 ”批量定时发布“菜单即可使用

== Screenshots ==
1. PluginUI
2. 插件使用界面

<?php 
/*
*Plugin Name: WP-Auto-Publish
*Plugin URI: http://moligu.com/wp-auto-publish/
*Description: 灵活设定自动批量定时发布文章。
*Version: 1.0
*Author: flashcol
*Author URI: http://moligu.com/
*License: GPL v2 or later
*Text Domain: wp-auto-publish
*Domain Path: /lang
*/
if ( ! defined( 'ABSPATH' ) ) exit; 
class WP_Auto_Publish{
	function __construct(){		
		add_action( 'admin_menu',array($this,'menu_page') );		//加入管理菜单
		add_filter( 'cron_schedules',array($this,'add_custom_cron_intervals') );		//自定义计划任务重复时间间隔
		add_action( 'WIND_cron_draft_update_hook',array($this,'WIND_cron_draft_update') );		//加入执行计划任务调用
		add_action( 'plugins_loaded', array($this,'wind_load_plugin_textdomain' ));
	}
function wind_load_plugin_textdomain() {
    load_plugin_textdomain( 'wp-auto-publish', FALSE, basename( dirname( __FILE__ ) ) . '/lang/' );
}

	//add menu page
	function menu_page(){
		//add_menu_page( 'WP-auto-publish','自动发布','manage_options','WP-auto-publish',array($this,'cron_form'),plugins_url('icon.png',__FILE__) );
		add_menu_page( 'WP-auto-publish',__( 'Auto Publish' , 'wp-auto-publish' ),'manage_options','WP-auto-publish',array($this,'cron_form'));
	}
	function cron_form(){
		include_once('form.php');
	}

	//自定义计划任务重复时间间隔
	function add_custom_cron_intervals($schedules){
		$recurrence=intval(get_option('WIND_recurrence_times'));
		
		switch (get_option('WIND_recurrence'))
		{
		case 'daily':
 		 	$recurrence= $recurrence*DAY_IN_SECONDS;
 		 	break;  
		case 'hourly':
  			$recurrence= $recurrence*HOUR_IN_SECONDS;
  			break;
		case 'weekly':
 		 	$recurrence= $recurrence*WEEK_IN_SECONDS;
 		 	break;  
		default:
  			$recurrence= $recurrence*DAY_IN_SECONDS;
		}
		$schedules['WIND_custom_recurrence'] = array(
			'interval' =>$recurrence,
			'display' => '定时'
		);
		return $schedules;	
	}	

	//设定草稿发布计划
	function WIND_cron_draft_update(){
		query_posts(array('posts_per_page'=>get_option('WIND_draft_num'),'orderby'=>get_option('WIND_draf_orderby'),'post_status'=>'pending','post_type'=>'post','order'=>'ASC'));
		$num=0;
		while(have_posts()){
			$ndate=date('Y-m-d H:i:s',time()+$num*(intval(get_option('WIND_interval_time'))));
			$ndate_gmt=get_gmt_from_date($ndate);
			
			the_post();
			kses_remove_filters();
			wp_update_post(array('ID'=>get_the_ID(),'post_status'=>'future','post_date'=>$ndate,'post_date_gmt'=>$ndate_gmt));
			kses_init_filters();
			$num++;
		}
		wp_reset_query();
		//wp_clear_scheduled_hook( 'WIND_cron_draft_update_hook' );
		//wp_schedule_event( strtotime("+1 day",strtotime(date("Y-m-d")." ".$starTime)),'WIND_custom_recurrence','WIND_cron_draft_update_hook' );
	}

}
//设置默认时区:
date_default_timezone_set( get_option('timezone_string') );
new WP_Auto_Publish();

这个代码需要添加到你的WordPress主题的functions.php文件中或者作为一个独立的插件来使用。

注意: 这个代码是一个简单的示例,可能无法满足所有的使用场景。在使用之前,你可能需要根据你的实际情况对代码进行修改和优化。在生产环境中使用之前,你需要对其进行充分的测试。

以下是如何安装WordPress插件的基本步骤:

  1. 首先,使用文本编辑器(如Notepad++或Sublime Text)打开一个新文件,将上述完整代码复制粘贴到该文件中。

  2. 将该文件保存为 auto_publish.php。确保文件扩展名是 .php

  3. 创建一个新的ZIP文件,将 auto_publish.php 添加到该ZIP文件中。你可以将ZIP文件命名为 auto_publish.zip

  4. 登录到你的WordPress网站后台,然后导航到 插件 > 添加新

  5. 点击 上传插件 按钮,然后选择刚才创建的ZIP文件 auto_publish.zip,然后点击 现在安装

  6. WordPress会自动处理上传并安装插件的过程。安装完成后,点击 激活插件 按钮来启用插件。

注意:在使用该插件之前,你可能需要修改插件代码中的部分设置,比如你希望自动发布的文章类别的slug。在激活插件前,请确保已经根据你的实际需求修改了代码。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享