Giter Site home page Giter Site logo

wordpress-using-composer's Introduction

wordpress-using-composer

把WordPress作为一个依赖

在项目中,把 WordPress 作为一个依赖对待,能保持Git仓库的整洁和模块化。无须维护、提交WordPress到Git仓库,以及能够更加方便地升级。 可以通过WP-CLI或者 Composer 将WordPress安装在子目录里。 以下以Composer为例,也可以用Git Submodules的方式实现。

调整WordPress结构

结构图如下: directory structure

  • 调整 index.php 位置

我们将会把WordPress安装在 public/wp 子目录,所以需要调整 index.php 的位置为 public/index.php

  • 把wp-content移动到WordPress安装目录外边

使用WordPress开发时,通常只需要改动 wp-content,所以把wp-content移动到public目录下,并调整 WP_CONTENT_DIRWP_CONTENT_URL 的值,见 public/wp-config.php

  • 把配置文件wp-config.php移动到 public 目录下

wp-config.php 有时也会改动,通过WordPress源码可知,WordPress会优先加载上一层目录的 wp-config.php,所以可以把wp-config.php移动至public目录下,更新public/wp时,无须担心被覆盖。 同时,引进了 localproduction 环境的不同配置,详见 public/wp-config.php,并在其中注册 Compoer Auto Loader:

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

使用Composer安装WordPress

在根目录下,创建 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的另外两个特性:

WordPress新结构中的插件和主题

  1. 可以通过 Composer 从 WordPress Packagist 下载第三方的插件和主题
  2. 可以在 public/wp-content 中定制自己的插件和主题

此时,需配置好 public/wp-content/.gitignore 进行管理,通常Git仓库不需要保存第三方的代码,但需要保存自制的插件、主题代码,可参考 https://deliciousbrains.com/using-composer-manage-wordpress-themes-plugins/

Nginx的配置

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;,则需对 siteurlhome 的链接做以下配置,访问后台时,路径需加上wp

Apache的配置

使用虚拟主机作为服务器时,根目录通常会被限制为/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/

https://roots.io/using-composer-with-wordpress/

https://github.com/johnpbloch/wordpress

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.