N
N
Nikita2019-10-08 16:57:54
git
Nikita, 2019-10-08 16:57:54

How to move wp-config.php and wp-content outside the wordpress core folder?

The essence is simple to understand, for development on a local machine, I want to use one WordPress core for all my projects - that is, themes. I plan to store the themes themselves in a different location on the local machine, and each time switch the "active" theme by changing the paths in the WP core configs using a gallp. Topics will accordingly be under git'a versioning.
The WordPress core itself is located in the local server folder C:\xampp\wordpress\www with VirtualHost configured for it from Apache - wordpress.local.The projects are in the C:\gulp\projects folder, there are projects with the usual static layout, and right there I plan to place folders with WordPress themes, inside the "theme" folder there will be wp-config.php and wp-content, and here I plan to place their paths directly in the WordPress core, so that WordPress picks them up and shows my theme with the correct configs to the database and each time I switch them using a gallp by changing the paths in the kernel configs.
That is, my goals are not to drag the core of the VP into the folder with the project every time (and there will be many such projects), the second is to track changes only in wp-config.php and wp-content with the git, the third is to work only under one wordpress domain. localand switch topics using path substitution, I don’t want to manually edit Apache configs every time and create a new virtual host, new folders for each project, restart Apache, etc. This is done only for local development of themes, that is, I plan to make the deployment "normal", without these changes and separation with the removal of folders.
Unfortunately, I did not find the necessary recipe on the Internet, just for myself. Specifically, I already tried to substitute the absolute path for wp-content in the core configs - define( 'WP_CONTENT_DIR', 'C:\gulp\projects\test wp\wp-content' ), but this unfortunately did not work, when the site loads, only White screen. I set WP_DEBUG to true, and for the C:\gulp\projects folder in Apache, I set Require all granted. As a result, I can log into the admin panel normally,
The question is, how to properly organize the connection of the external wp-config.php file and the external wp-content folder with all its contents to the WordPress core, located elsewhere on the hard drive? What additional settings need to be registered in Apache? Who faced such a problem or who knows how to solve - share your experience :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2020-09-11
@neins

After a long time, I'm posting my solution. As it turned out, WordPress supports this solution almost out of the box and doing all this is not so difficult.
The scheme is as follows: there is a folder with the WP core and there are folders with projects. In my case, the core folder is C://localhost/wordpress.local , and the project folder is C://dev/projects/example/(static | wp) .
The folder with the core contains only the necessary components for the functioning of the CMS and we hang the wordpress.local domain on this folder, here we only slightly change the wp-config file - it will be designed to connect the external folder wp-content of our project and data to connect to the database , remove the wp-content folder from the core folder as unnecessary.
Inside the project folder there are two folders static and wp, in our case we need a wp folder which in turn will already contain the wp-content folder we need and the wp-config-dev.php and wp-config.php files . The data for connecting to the database for local development will be located in wp-config-dev, and we will deploy wp-config.php, the content of which does not differ from the usual WordPress config file.
From the additional it is necessary to share our folder with projects as a domain for the WP_CONTENT_URL' constant in the virtual host settings.
The contents of wp-config.php core Wordpress

<?php
$project = 'example';
define( 'WP_CONTENT_DIR', 'C://DEV/projects/'.$project.'/wp/wp-content' );
define( 'WP_CONTENT_URL', 'http://projects.folder/'.$project.'/wp/wp-content' );
include('C://DEV/projects/'.$project.'/wp/wp-config-dev.php');
if ( ! defined( 'ABSPATH' ) ) {
  define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once( ABSPATH . 'wp-settings.php' );

The contents of wp-config-dev.php of the Wordpress project
<?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_';

Virtual host file httpd-vhosts.conf
<Directory "C://DEV/projects/*">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

# Для WP_CONTENT_URL
<Directory "C://DEV/projects">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>
<VirtualHost *:80>
    DocumentRoot "C://DEV/projects"
    ServerName projects.folder
    # Заклинание против CORS
    Header set Access-Control-Allow-Origin "*"
</VirtualHost>

As a result, we separate the WordPress core from a specific project - you can easily switch between projects just by changing the value of the $project variable in the core config (I do this using a shell script, I already use them in gulpa and other places, here it’s more convenient for you) and only the folder with the project (theme-plugins-downloads, etc.) is also versioned and deployed, the kernel is not dragged into the git and it can also be downloaded to the host separately.

I
Igor Vorotnev, 2019-10-08
@HeadOnFire

https://github.com/ihorvorotnov/sonata
I haven't updated the code for a long time, a lot needs to be refreshed, but I think the concept is clear.
What is important to understand is to simply decompose the topic separately in different places and everything else will not work, except perhaps with symlinks. The kernel will look for the config 2 levels up, some paths (content, as you already understood) will be counted from the root (where the config is).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question