Pollen Wordpress Env 组件 - 使用 .env 文件进行 Wordpress 配置。

v1.0.6 2022-12-29 00:00 UTC

This package is auto-updated.

Last update: 2024-09-29 06:08:40 UTC


README

Latest Stable Version MIT Licensed PHP Supported Versions

Pollen Solutions Wordpress Env 组件提供了一种简单的方式来配置您的 Wordpress 应用程序。

安装

composer require pollen-solutions/wp-env

将 wp-config.php 文件的内容替换为以下代码

use Pollen\WpEnv\WpEnv;

// Optionnal but recommended start time global indicator
defined('START_TIME') ?: define('START_TIME', microtime(true));

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

new WpEnv(__DIR__);

require_once(ABSPATH . 'wp-settings.php');

配置

.env 配置文件

基础知识

在项目的根目录创建一个 .env 文件

# ENVIRONMENT
## Environnement of your application. 
## dev|prod. 
APP_ENV=dev

## Enabling debug
APP_DEBUG=true

## Url of your application
APP_URL=https://127.0.0.1:8000

## Application timezone
APP_TIMEZONE=Europe/Paris

# PATH (Optionnal)
## Relative public path of your application 
APP_PUBLIC_DIR=/
## Relative path to the wordpress root folder, containing wp-admin and wp-includes folder 
APP_WP_DIR=/
## Relative path to the wp-content root folder, containing languages, themes, uploads ...
## MUST BE WRITABLE BY HTTP SERVER 
APP_WP_PUBLIC_DIR=wp-content

# DATABASE
## DATABASE DRIVER
DB_CONNECTION=mysql
## DATABASE HOST
DB_HOST=127.0.0.1
## DATABASE PORT
DB_PORT=3306
## DATABASE NAME
DB_DATABASE=wordpress
## DATABASE USERNAME
DB_USERNAME=root
## DATABASE PASSWORD
DB_PASSWORD=root
## DATABASE TABLES PREFIX
DB_PREFIX=wp_

# WORDPRESS CONFIG
# @see https://wordpresstheme.cn/support/article/editing-wp-config-php/
WP_DEBUG_LOG=true
DISALLOW_FILE_MODS=true
AUTOMATIC_UPDATER_DISABLED=false
DISABLE_WP_CRON=false

## WORDPRESS SALT
## @see https://developer.wordpress.org/reference/functions/wp_salt/
## @see https://api.wordpress.org/secret-key/1.1/salt/
## Generate salt from cli :
## php vendor/bin/wp-salt dotenv --clean >> .env

嵌套变量

可以在另一个环境中嵌套环境变量,这有助于减少重复。

通过将现有环境变量用 ${…} 括起来来实现,例如。

BASE_DIR=/var/webroot/project-root
CACHE_DIR=${BASE_DIR}/cache
TMP_DIR=${BASE_DIR}/tmp

覆盖

通过 .env.local 文件,可以在另一个环境中覆盖环境变量。

# > .env file
BASE_DIR=/var/webroot/common-project-root
# > .env.local file
BASE_DIR=/var/webroot/local-project-root

.wp-config.local 配置文件

与原始的 wp-config.php 文件相同,位于项目根目录的 wp-config.local.php 文件允许定义您的 Wordpress 应用程序的常量。

use Pollen\Support\Env;
use Pollen\Support\Filesystem as fs;

// LOGS
if (!defined('WP_DEBUG_LOG')) {
    define('WP_DEBUG_LOG', getcwd() . fs::DS . 'var' . fs::DS . 'log' . fs::DS . 'error.log');
}

// AUTHENTICATION
if (!defined('COOKIE_DOMAIN')) {
    define('COOKIE_DOMAIN', Env::get('DOMAIN_CURRENT_SITE'));
}
if (!defined('COOKIEPATH')) {
    define('COOKIEPATH', '/');
}
if (!defined('SITECOOKIEPATH')) {
    define('SITECOOKIEPATH', '/');
}