首页 > 解决方案 > Linux + Wordpress:如何从 Linux Cron 作业中触发函数?

问题描述

我创建了一个 Linux Cron 作业,例如:

0 0 * * * home/www/wp-content/themes/my_theme/functions.php

文件 wp-content/themes/my_theme/functions.php 具有我需要从 cron 作业调用的函数:do_something()。

如您所见,命令是:home/www/wp-content/themes/my_theme/functions.php是错误的,因为我需要指定我的函数:do_something()。

我的问题是:如何更改作业命令来执行函数 do_something()。

标签: linuxwordpress

解决方案


(1) 如果您要使用 wordpress 特定功能,请不要直接为 cron 作业调用 functions.php 文件,而是首先使用我在您发布的上一个问题中提到的两个命令设置 wordpress 环境。

<?php 
define('WP_USE_THEMES', false);
require('./wp-blog-header.php'); 
//ensure correct *path* for your wp-blog-header.php file
//now you can use function you want from functions.php file directly
do_something();
?>


(2) 如果您的 do_something() 函数不适用于任何与 wordpress 相关的任务,只需复制该函数并在您的 cron 文件中使用它,例如 mycron.php

(3) 您曾询问将建议的代码放在哪里。将建议的代码放在 mycron.php 文件的顶部。


推荐阅读