jalsoedesign/climax-wp

CLImax的Wordpress集成

安装: 1

依赖: 0

建议者: 0

安全性: 0

星级: 0

关注者: 2

分支: 0

开放问题: 0

类型:package

dev-master 2020-03-05 09:12 UTC

This package is auto-updated.

Last update: 2024-09-05 19:40:53 UTC


README

CLImax的WP界面。只需创建一个扩展WordpressApplication的文件并启动它。它已经运行了所有WP代码,所有WP函数都将可用。

wp-content/plugins/custom/cli.php:

<?php

use CLImax\Wordpress\WordpressApplication;

require_once(__DIR__ . '/../vendor/autoload.php');

class CliApplication extends WordpressApplication {
   public function init() {
       $this->info(sprintf('Looking up site URL in wordpress..'));
       $siteUrl = $this->getWpDb()->get_var('SELECT option_value FROM wp_options WHERE option_name = "siteurl"');

       $this->success(sprintf('ABSPATH value: {{%s}}', ABSPATH));
       $this->success(sprintf('Site URL: {{%s}}', $siteUrl));
   }
}

CliApplication::launch();

..及其输出

C:\git\climax-wp-test\wp-content\plugins\custom>php cli.php

----------------------------------------------------------------------------------------------------[START]----------------------------------------------------------------------------------------------------08:59:07,8955 INFO: Looking up site URL in wordpress..
08:59:07,8972 SUCCESS: ABSPATH value: C:\git\climax-wp-test\
08:59:07,0898 SUCCESS: Site URL: https://climax-wp-test.localhost
-----------------------------------------------------------------------------------------------------[END]-----------------------------------------------------------------------------------------------------Script ImportApplication ended at 2020-03-05 08:59:07

It took 0 hours, 0 minutes, 0.7750 seconds

特殊方法

protected function __getWpRoot()

如果应用不在wp-content文件夹内,可能需要在应用内部重写__getWpRoot()方法。默认情况下,该函数会搜索父路径以找到/wp-content/

protected function __getWpRoot() {
    return __DIR__; // Assuming the cli file is placed in the root of the webserver
}

protected function getWpDb()

getWpDb()方法返回一个没有使用$GLOBALSglobal $wpdb\wpdb实例。