【问题标题】:Run W3 Total Cache Function with Crontab使用 Crontab 运行 W3 总缓存功能
【发布时间】:2012-01-31 06:44:11
【问题描述】:

好的。我真的被这个难住了。

基本上,我需要调用 Wordpress 插件 W3 Total Cache 的函数作为 crontab 中 cron 作业的一部分。我想每晚自动清除整个页面缓存。

这是我需要调用的在 wordpress 中运行良好的代码:

if (function_exists('w3tc_pgcache_flush')) {
w3tc_pgcache_flush();
} 

我目前正在使用以下脚本:

#!/usr/bin/php

<?php

define('DOING_AJAX', true);
define('WP_USE_THEMES', false);
$_SERVER = array(
    "HTTP_HOST" => "http://example.com",
    "SERVER_NAME" => "http://example.com",
    "REQUEST_URI" => "/",
    "REQUEST_METHOD" => "GET"
);
require_once('/path-to-file/wp-load.php');

wp_mail('email@example.com', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');

if (function_exists('w3tc_pgcache_flush')) {
w3tc_pgcache_flush();
} 

?>

和命令行:

php -q /path-to-file/flushtest.php

我使用 wp_mail 功能进行测试并确保我得到了一些东西。

脚本运行良好,只是页面缓存从不刷新。我收到了电子邮件,日志中也没有任何错误。

有什么想法吗?

感谢您的帮助。

【问题讨论】:

  • crontab 条目因其默认使用的最小路径而臭名昭著。除非您在 crontab 或调用脚本中为 PHP 代码设置 PATH,否则我认为它无法找到您的 php 库。为了测试这个想法,设置一个 else 条件来打印 w3tc_pgcache_flush 不存在的警告?祝你好运。
  • 嗯...可能就是这样。我当然使用 wp-load.php 文件的完整路径。但它似乎找不到运行 w3tc 函数所需的内容。我认为 wp-load.php 将包含该函数访问 wordpress 插件文件所需的一切。关于我可能需要什么其他文件的任何想法?

标签: php wordpress plugins command-line crontab


【解决方案1】:

现在更好的版本是使用wp-cli。最新版本(0.9.2.8)与此插件兼容。只需从 wordpress 目录中的任何位置运行此命令:

wp w3-total-cache flush

【讨论】:

    【解决方案2】:

    如果仍然有效,请稍微更改一下顺序:

    w3tc_pgcache_flush(); # let it crash here so that you won't get the mail
    wp_mail('email@example.com', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');
    

    【讨论】:

    • 好吧,还是不行。但即使我换了订单,我也确实收到了两次电子邮件。也许我需要别的东西来调用那个插件函数?
    • 但是您在远程调试下的安装,启动 WP_CRON 请求并逐步查看会发生什么。还要检查插件是否能够通过一些报告或返回值来说明错误和不匹配的先决条件。
    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多