在WP插件上运行cron作业

时间:2015-04-23 11:13:42

标签: php wordpress cron

我稍微修改了一个wp-plugin,以便让它自动运行。

地址为http://www.example.com/wp-admin/admin.php?page=wp_posts&add=add

问题是我不能让它工作,而我的cron在使用它时会给我一个错误。

我用

打电话
wget -O /dev/null http://www.example.com/wp-admin/admin.php?page=wp_posts&add=add

因此我得到了

--2015-04-23 10:00:01--  http://www.example.com/wp-admin/admin.php?page=wp_posts
Resolving www.example.com... 46.105.40.207
Connecting to www.example.com|46.105.40.207|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://www.example.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fwp-admin%2Fadmin.php%3Fpage%3Dwp_posts&reauth=1 [following]
--2015-04-23 10:00:03--  http://www.example.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fwp-admin%2Fadmin.php%3Fpage%3Dwp_posts&reauth=1
Reusing existing connection to www.example.com:80.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: “/dev/null”

 0K ...                                                     237M=0s

 2015-04-23 10:00:03 (237 MB/s) - “/dev/null” saved [3180]

所以似乎由于这是管理员限制的,我无法运行它。

关于我如何改变它的任何想法?

1 个答案:

答案 0 :(得分:3)

您可以在 functions.php 文件中使用此代码段,然后您可以通过公共网址运行它,例如http://example.com/cron

function my_CronJob() {
    global $wp;
    // Check that we are getting "cron" slug from url.
    if($wp->request == 'cron'){
        // Run you cron script here.  

        die();
    }
}

add_filter('template_redirect', 'my_CronJob');

或者,如果您想使用wordpress实际标准,可以查看article

希望它对你有所帮助。