简体   繁体   English

阿帕奇。 如何将 wp-config.php 和 wp-content 从 wordpress 核心移出?

[英]Apache. How to move wp-config.php and wp-content outside from wordpress core?

I want to use one wordpress core for all my themes when i develop them on my local machine.当我在本地机器上开发它们时,我想为所有主题使用一个 wordpress 核心。 For example wordpress core located on C:\\xampp\\wordpress\\www and it has a VirtualHost from Apache https://wordpress.local .例如,位于C:\\xampp\\wordpress\\www上的 wordpress 核心,它有一个来自 Apache https://wordpress.local的 VirtualHost。 Themes located on C:\\gulp\\projects and here is located all projects (folders) with static websites and here is i want to store all my wp themes in their own folder.主题位于C:\\gulp\\projects ,这里是所有带有静态网站的项目(文件夹),这里是我想将所有 wp 主题存储在它们自己的文件夹中。 Inside of each wp theme folder is wp-config.php and the wp-content folder.每个 wp 主题文件夹内是wp-config.phpwp-content文件夹。 I want to store each wp theme folder on git versioning and i planning switch projects by gulp just by replacing paths in wp configs in the core.我想将每个 wp 主题文件夹存储在 git versioning 上,我计划通过 gulp 切换项目,只需替换核心中 wp 配置中的路径。 I plan to do this only on local machine and only for development reasons and i just want push changes on server.我计划仅在本地机器上执行此操作,并且仅出于开发原因,我只想在服务器上推送更改。 I don't want to create a new virtual domain and folders in apache for each new project and i don't want to grab wordpress core to git versioning.我不想为每个新项目在 apache 中创建一个新的虚拟域和文件夹,我不想将 wordpress 核心抓取到 git 版本控制。

I tried to change paths in configs in the core define( 'WP_CONTENT_DIR', 'C:\\gulp\\projects\\TEST-WP-THEME\\wp-content' ) , but unfortunately wp core can't "see" the directory and there is white screen only when i go to my domain in the browser.我试图在核心define( 'WP_CONTENT_DIR', 'C:\\gulp\\projects\\TEST-WP-THEME\\wp-content' )更改配置中的路径define( 'WP_CONTENT_DIR', 'C:\\gulp\\projects\\TEST-WP-THEME\\wp-content' ) ,但不幸的是 wp 核心无法“看到”目录和那里只有当我在浏览器中访问我的域时才会出现白屏。 I suppose that apache can't "see" directories outside C:\\xampp\\wordpress\\www , but i believe that it is possible to configure apache that it may hook up directories outside from "server".我想 apache 无法“看到” C:\\xampp\\wordpress\\www之外的目录,但我相信可以配置 apache 使其可以连接“服务器”之外的目录。 How to hook up external wp-config.php and the wp-content folder to the wp core, who knows how to do this please help me.如何将外部wp-config.phpwp-content文件夹连接到 wp 核心,谁知道如何做到这一点,请帮助我。

After long time i publish my solution.很长一段时间后,我发布了我的解决方案。 For example: the WP core will be stored in C://wordpress.local/ folder and for that folder we'll create a http://wordpress.local domain.例如:WP 核心将存储在C://wordpress.local/文件夹中,我们将为该文件夹创建一个http://wordpress.local域。 All projects will be in C://projects/ .所有项目都将在C://projects/ Each project has its own wp-content folder, wp-config-dev.php (for DB credentials and table prefix) and usual wp-config.php that we deploy on the server.每个项目都有自己的wp-content文件夹, wp-config-dev.php (用于数据库凭据和表前缀)和我们部署在服务器上的常用wp-config.php

We'll take a project called example in C://projects/example/wp .我们将在C://projects/example/wp一个名为example C://projects/example/wp

Core's wp-config.php Core 的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');

Project's wp-core-dev.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_';

In additional you have to share your projects folder as domain in Apache's httpd-vhosts.conf此外,您必须在 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>

And voila, you may switch between projects just with changing the $project variable, personally i do it with bash script and sed.瞧,您可以通过更改$project变量在项目之间切换,我个人使用 bash 脚本和 sed 来完成。


Bonus.奖金。 Put all our projects in WP admin bar and make project selector.将我们所有的项目放在 WP 管理栏中并制作项目选择器。

Add function to your .bashrc将函数添加到您的.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
}

Edit optional custom.php file that we include in core's 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();
}

Eventually we got a nice and convenient project selector:最终我们得到了一个漂亮而方便的项目选择器:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM