很长一段时间后,我发布了我的解决方案。例如:WP 核心将存储在C://wordpress.local/ 文件夹中,我们将为该文件夹创建一个http://wordpress.local 域。所有项目都将在C://projects/。每个项目都有自己的wp-content 文件夹、wp-config-dev.php(用于数据库凭据和表前缀)以及我们在服务器上部署的常用wp-config.php。
我们将在C://projects/example/wp 中处理一个名为example 的项目。
核心的wp-config.php
<?php
$project = 'example';
define( 'WP_CONTENT_DIR', 'C:/projects/'.$project.'/wp/wp-content' );
define( 'WP_CONTENT_URL', 'https://projects.folder/'.$project.'/wp/wp-content' );
include('C:/projects/'.$project.'/wp/wp-config-dev.php');
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once( ABSPATH . 'wp-settings.php' );
// custom functions (optional)
include('custom.php');
项目的wp-core-dev.php
<?php
define( 'DB_NAME', 'example' );
define( 'DB_USER', 'root' );
define( 'DB_PASSWORD', '' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8mb4' );
define( 'DB_COLLATE', '' );
$table_prefix = 'wp_';
此外,您必须在 Apache 的 httpd-vhosts.conf 中将您的项目文件夹共享为域
<Directory "C://projects/*">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
# For WP_CONTENT_URL
<Directory "C://projects">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot "C://projects"
ServerName projects.folder
# CORS
Header set Access-Control-Allow-Origin "*"
</VirtualHost>
瞧,您可以通过更改 $project 变量在项目之间切换,我个人使用 bash 脚本和 sed 来完成。
奖励。将我们所有的项目放在 WP 管理栏中并制作项目选择器。
将函数添加到您的.bashrc
function wp-set-project {
local projname="$*"
# in my case $project is always on second line
sed -i "2s|.*|\$project = '$projname';|" /c/wordpress.local/wp-config.php
}
编辑我们包含在核心的wp-config.php 中的可选custom.php 文件
add_action('admin_bar_menu', 'add_project_switch', 999);
function add_project_switch($admin_bar){
//get current project name from $project variable, read second line from file
preg_match('/\'(.*?)\'/', file('C://wordpress.local/wp-config.php')[1], $result);
$currWpProject = $result[1]; //result from second mask without quotes
//get all projects
$wpProjects = array();
foreach(glob('C://projects/*/wp', GLOB_ONLYDIR) as $proj) {
$wpProjects[] = explode('/', $proj)[3]; //get dir name
}
$admin_bar->add_menu( array(
'id' => 'curr-proj',
'title' => '? Project: '.$currWpProject,
));
foreach($wpProjects as $proj) {
if ($proj !== $currWpProject) {
$admin_bar->add_menu( array(
'id' => 'project-'.$proj,
'parent' => 'curr-proj',
'title' => $proj,
'href' => '?setWp='.$proj, //make link with get parametr
));
}
}
}
//Call our bash function if there is passed GET with project name
$setWp = filter_input( INPUT_GET, 'setWp');
if($setWp){
//for running bash that way in Windows you have to put your bash.exe in PATH
shell_exec ('bash -c "source ~/.bashrc && wp-set-project '.$setWp.'"');
header("Location: "."http://".$_SERVER['HTTP_HOST']);
exit();
}
最终我们得到了一个不错且方便的项目选择器: