WordPress 定时发布
做seo的经常进行数据采集工作,但是大规模的数据采集后进行发布,往往会导致百度K站,所以实现了定时自动发布文章的功能
/** * 定时发布 插件 */ if(!function_exists('add_action')){ header('Status 403 Forbidden'); header('HTTP/1.0 403 Forbidden'); header('HTTP/1.1 403 Forbidden'); exit();} /** * 定义发布时间间隔 单位分钟 * WPMS_DELAY */ define('WPMS_DELAY',1); define('WPMS_OPTION','wp_missed_schedule'); function wpms_replace(){ delete_option(WPMS_OPTION); } register_deactivation_hook(__FILE__,'wpms_replace'); function wpms_init(){ remove_action('publish_future_post','check_and_publish_future_post'); $last=get_option(WPMS_OPTION,false); if(($last!==false)&&($last>(time()-(WPMS_DELAY*60))))return; update_option(WPMS_OPTION,time()); global$wpdb; /** * 获取需要发布的文章,从draft到publish状态 */ $scheduledIDs=$wpdb->get_col("SELECT`ID`FROM`{$wpdb->posts}`"."WHERE("."((`post_date`>0)&&(`post_date`<=CURRENT_TIMESTAMP()))OR"."((`post_date_gmt`>0)&&(`post_date_gmt`<=UTC_TIMESTAMP()))".")AND`post_status`='draft'LIMIT 0,1"); if(!count($scheduledIDs))return; foreach($scheduledIDs as$scheduledID){if(!$scheduledID)continue; wp_publish_post($scheduledID);} } add_action('init','wpms_init',0);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/
*
*
*
定时发布
插件
*
/
if
(
!
function_exists
(
'add_action'
)
)
{
header
(
'Status 403 Forbidden'
)
;
header
(
'HTTP/1.0 403 Forbidden'
)
;
header
(
'HTTP/1.1 403 Forbidden'
)
;
exit
(
)
;
}
/
*
*
*
定义发布时间间隔
单位分钟
*
WPMS_DELAY
*
/
define
(
'WPMS_DELAY'
,
1
)
;
define
(
'WPMS_OPTION'
,
'wp_missed_schedule'
)
;
function
wpms_replace
(
)
{
delete_option
(
WPMS_OPTION
)
;
}
register_deactivation_hook
(
__FILE__
,
'wpms_replace'
)
;
function
wpms_init
(
)
{
remove_action
(
'publish_future_post'
,
'check_and_publish_future_post'
)
;
$
last
=
get_option
(
WPMS_OPTION
,
false
)
;
if
(
(
$
last
!==
false
)
&&
(
$
last
>
(
time
(
)
-
(
WPMS_DELAY
*
60
)
)
)
)
return
;
update_option
(
WPMS_OPTION
,
time
(
)
)
;
global
$
wpdb
;
/
*
*
*
获取需要发布的文章
,从
draft到
publish状态
*
/
$
scheduledIDs
=
$
wpdb
->
get_col
(
"SELECT`ID`FROM`{$wpdb->posts}`"
.
"WHERE("
.
"((`post_date`>0)&&(`post_date`<=CURRENT_TIMESTAMP()))OR"
.
"((`post_date_gmt`>0)&&(`post_date_gmt`<=UTC_TIMESTAMP()))"
.
")AND`post_status`='draft'LIMIT 0,1"
)
;
if
(
!
count
(
$
scheduledIDs
)
)
return
;
foreach
(
$
scheduledIDs
as
$
scheduledID
)
{
if
(
!
$
scheduledID
)
continue
;
wp_publish_post
(
$
scheduledID
)
;
}
}
add_action
(
'init'
,
'wpms_init'
,
0
)
;
|
亲测 效果还是可以的!!!,采集有风险需谨慎啊。
1313

被折叠的 条评论
为什么被折叠?



