gpt4 book ai didi

php - 如何从 wordpress 中的 cli 启动 php 脚本

转载 作者:行者123 更新时间:2023-12-04 17:54:43 24 4
gpt4 key购买 nike

我写了一个 PHP 脚本,它是一个 WP-Cron-cronjob,它使用 wordpress 特定的功能。由于其运行时环境的某些限制,我需要使用 /usr/bin/php -q longThing.php 从命令行启动此脚本,而不是作为 WP-Cron 事件。我如何确保所有 wordpress 核心函数都可以在我的脚本中调用?

最佳答案

Xaedes solution效果很好:

<?php
if( php_sapi_name() !== 'cli' ) {
die("Meant to be run from command line");
}

function find_wordpress_base_path() {
$dir = dirname(__FILE__);
do {
//it is possible to check for other files here
if( file_exists($dir."/wp-config.php") ) {
return $dir;
}
} while( $dir = realpath("$dir/..") );
return null;
}

define( 'BASE_PATH', find_wordpress_base_path()."/" );
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

关于php - 如何从 wordpress 中的 cli 启动 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com