每天使用wordpress执行php脚本 [英] Execute php script daily with wordpress

查看:67
本文介绍了每天使用wordpress执行php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个检查URL状态的脚本。现在,我正在尝试将其与wordpress集成。这是代码:

I found a script that check status of a url. Now i'm trying to integrate it with wordpress. This is the code:

<?php
/*
Plugin Name: checkOnline Status
Version: 0.1
*/

add_action( 'cO_cron_hook', 'CheckRemoteService' );

if( !wp_next_scheduled( 'cO_cron_hook' ) ) {
wp_schedule_event( time(), 'daily', 'cO_cron_hook' );
}

register_deactivation_hook( __FILE__, 'bl_deactivate' );

function bl_deactivate() {
$timestamp = wp_next_scheduled( 'cO_cron_hook' );
wp_unschedule_event($timestamp, 'cO_cron_hook' );
}

function CheckRemoteService($atts) {
extract(shortcode_atts(array(
   'url' => 'http://',
   'cache' => '600', // 60*10 (10 minutes)
   'online' => 'Online', // Custom online msg
   'offline' => 'Offline' // Custom online msg
), $atts));

$CachedStatus = 'cstatus_' . $url;
$cachedposts = get_transient($CachedStatus);
if ($cachedposts !== false) {
return $cachedposts;
} else {

// Sometimes its best to change to a custom agent message
// so you know where requests are coming from.

$agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
$ch = curl_init();
 curl_setopt ($ch, CURLOPT_URL,$url );
 curl_setopt($ch, CURLOPT_USERAGENT, $agent);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch,CURLOPT_VERBOSE,false);
 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
 curl_setopt($ch, CURLOPT_NOBODY, 1);
 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt($ch,CURLOPT_SSLVERSION,3);
 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
 curl_exec($ch);
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   if($httpcode >= 200 && $httpcode < 400) {
    return $online;
    } else {
    return $offline;
    }

 set_transient($CachedStatus, $return, $cache); 
return $return;
}
}
add_shortcode('checkmyurl','CheckRemoteService');

?>

我想要的是每天只运行一次此代码。刷新或访问使用简码的页面时,如何使它不运行?我需要显示大约50个站点的状态,并且页面需要花费10秒钟以上的时间才能加载。
很抱歉,这个简单的问题(我对此不满意),但找不到解决方案。
谢谢。

What i want is run this code only once daily. How can i make it not run when refresh or visit a page where i use shortcodes ? I need display status of about 50 sites and page takes more than 10 sec to load now. Sorry for this simple question (i'm a noob to this) but i can't find a solution. Thanks.

编辑。使用 http://wordpress.org/plugins/crony/

推荐答案

您可以使用 wp_schedule_event()安排重复执行的任务。

You can use wp_schedule_event() to schedule recurring tasks.

wp_schedule_event() 文档):


安排一个钩子,该钩子将由WordPress操作核心在特定间隔内执行,由您指定。如果预定时间已过,则该操作将在有人访问您的WordPress网站时触发。请参阅Plugin API中的钩子列表。

Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. See the Plugin API for a list of hooks.

要每天安排活动,您可以执行以下操作:

To schedule an event daily, you'd do something like this:

function someFunc() {
    wp_schedule_event( time(), 'daily', 'mydailyEventHook');
}

希望这会有所帮助!

这篇关于每天使用wordpress执行php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆