Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active February 11, 2019 20:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CMCDragonkai/468c7ea7a1a2e9eb386fb0626fafc0c3 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/468c7ea7a1a2e9eb386fb0626fafc0c3 to your computer and use it in GitHub Desktop.
Integrating Wordpress with Composer #php #wordpress

Integrating Wordpress with Composer

Setup the project directory:

mkdir wordpress-composer
cd wordpress-composer

You need a composer.json that looks like this:

{
  "name": "wordpress-composer",
  "repositories": [
    {
      "type": "composer",
      "url": "https://wpackagist.org"
    }
  ],
  "require": {
    "johnpbloch/wordpress": "^4.8.0",
    "wpackagist-plugin/w3-total-cache" : "^0.9.5.4"
  },
  "extra": {
    "wordpress-install-dir": "wp"
  },
  "autoload": {
    "files": []
  },
  "minimum-stability": "dev",
  "prefer-stable": true
}

Run composer install.

Then create a wp-config.php at root that looks like:

<?php

define('WP_ENV', 'development');
define('ABSPATH', __DIR__ . '/wp/');
define('WP_HOME', 'localhost');
define('WP_SITEURL', 'localhost');
define('WP_CONTENT_URL', 'localhost/wp-content');
define('WP_CACHE', false);
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);
define('WP_ALLOW_REPAIR', false);

define('DB_NAME', 'wordpress-composer');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '127.0.0.1');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
$table_prefix  = 'wp_';

// generate with https://api.wordpress.org/secret-key/1.1/salt/
define('AUTH_KEY',         '');
define('SECURE_AUTH_KEY',  '');
define('LOGGED_IN_KEY',    '');
define('NONCE_KEY',        '');
define('AUTH_SALT',        '');
define('SECURE_AUTH_SALT', '');
define('LOGGED_IN_SALT',   '');
define('NONCE_SALT',       '');

require_once('vendor/autoload.php');
require_once(ABSPATH . 'wp-settings.php');

Remember that Wordpress is not arranged in a front-controller pattern. This means anything constants and configuration that needs to exist at all places (like at wp-admin), must be placed in the wp-config.php.

Create an index.php like:

<?php

define('WP_USE_THEMES', true);
require(__DIR__ . '/wp/wp-blog-header.php');

Run cp -r wp/wp-content/themes wp-content/themes'.

Setup the database and configure your wp-config.php to connect to it.

Now run php -S "127.0.0.1:1337" -t .. Do not use index.php as the router! Wordpress is not arranged in the front-controller pattern.

Remember that by using the site url and home constants in wp-config.php, the site url and home options in the administration backend become useless and can be left as just localhost. The constants are better as you can specialise it to your environment.

Also you need a .gitignore:

/vendor/
/wp/
/wp-config.php
/wp-config.*.php
!/wp-config.sample.php
/wp-content/debug.log
/wp-content/cache/
/wp-content/uploads/
/wp-content/plugins/

The wp-config.php now acts as your environment file as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment