在项目中,把 WordPress 作为一个依赖对待,能保持Git仓库的整洁和模块化。无须维护、提交WordPress到Git仓库,以及能够更加方便地升级。 可以通过WP-CLI或者 Composer 将WordPress安装在子目录里。 以下以Composer为例,也可以用Git Submodules的方式实现。
- 调整
index.php
位置
我们将会把WordPress安装在 public/wp
子目录,所以需要调整 index.php
的位置为 public/index.php
- 把wp-content移动到WordPress安装目录外边
使用WordPress开发时,通常只需要改动 wp-content
,所以把wp-content移动到public目录下,并调整 WP_CONTENT_DIR
和 WP_CONTENT_URL
的值,见 public/wp-config.php
- 把配置文件wp-config.php移动到
public
目录下
wp-config.php
有时也会改动,通过WordPress源码可知,WordPress会优先加载上一层目录的 wp-config.php
,所以可以把wp-config.php移动至public目录下,更新public/wp时,无须担心被覆盖。
同时,引进了 local
和 production
环境的不同配置,详见 public/wp-config.php
,并在其中注册 Compoer Auto Loader:
require __DIR__ . '/../vendor/autoload.php';
在根目录下,创建 composer.json,然后 composer install,如:
{
"name": "eddiclin/wordpress-using-composer",
"description": "Use Composer within WordPress.",
"keywords": ["wordpress", "composer"],
"license": "MIT",
"type": "project",
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
"php": ">=5.4",
"composer/installers": "1.*",
"johnpbloch/wordpress": "4.5.*"
},
"extra": {
"wordpress-install-dir": "public/wp",
"installer-paths": {
"public/wp-content/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
"public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
"public/wp-content/themes/{$name}/": ["type:wordpress-theme"]
}
}
}
从composer.json的extra配置项,可以看到,这里应用了Composer的另外两个特性:
- 可以通过 Composer 从 WordPress Packagist 下载第三方的插件和主题
- 可以在
public/wp-content
中定制自己的插件和主题
此时,需配置好 public/wp-content/.gitignore
进行管理,通常Git仓库不需要保存第三方的代码,但需要保存自制的插件、主题代码,可参考 https://deliciousbrains.com/using-composer-manage-wordpress-themes-plugins/
server {
listen 80;
root /path/to/public;
server_name example.com;
index index.php index.html;
if (!-f $request_filename) {
rewrite ^/wp-(.*) /wp/wp-$1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 1d;
}
location ~ .*\.(js|css|html|htm)?$ {
expires 12h;
}
}
注:若nginx中无rewrite配置
rewrite ^/wp-(.*) /wp/wp-$1;
,则需对siteurl
和home
的链接做以下配置,访问后台时,路径需加上wp
- siteurl: http://example.com/wp (with /wp)
- home: http://example.com (without /wp)
使用虚拟主机作为服务器时,根目录通常会被限制为/htdocs
,若想把项目存放在子目录中,如/htdocs/project
,并且希望能够通过 http://example.com 访问,.htaccess
可做如下配置:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^wp-content/(.*) project/public/wp-content/$1 [END]
RewriteRule ^wp-(.*) project/public/wp/wp-$1 [END]
RewriteRule . project/public/index.php [L]
#END在Apache2.3.9之后才支持,若不支持,改`END`为`L`,上述最后一句RewriteRule替换为
#RewriteRule ^(?!project) project/public/index.php [L]
</IfModule>
配置知识参考:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html http://www.jinbuguo.com/apache/menu22/mod/mod_rewrite.html
https://deliciousbrains.com/install-wordpress-subdirectory-composer-git-submodule/