We all like WordPress, but personally I do not want to see all the main WordPress core files in the root of the site, because if manages multiple WordPress sites on the same web server, it is important to keep organized your installs, it looks messy and unprofessional or you may not want to install WordPress at the root of your website. In any case, fortunately, WordPress gives us the flexibility to install the WordPress core in a custom subdirectory. This adjustment gives us more security and also to avoid frequent pitfalls.
There are many ways to implement WordPress core in a Subdirectory, but WordPress itself has a great article on the Codex “Giving WordPress Its Own Directory”, but I thought do something a bit different with composer’s dependencies. In the last article, I explained the method of installing the composer and how to use it for WordPress Plugin and themes.
Please follow below example setup I am doing in my local environment with configuration Apache 2.4.18,PHP 7.0.22,MySQL 5.7.20 and Ubuntu 16.04.1, here i am creating a sitename as “scriphere” folder.
Video Tutorial
Here is the sample source code for “Install WordPress core in a Subdirectory using Composer”, If you do not like the video or if you need other steps, please continue reading.
1.Create a “script here” directory in your local host’s root directory.
2.Next, copy “composer.json” file to the application root folder (Ex: scripthere).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
{ "name": "scripthere/script", "description": "WordPress and everything else with Composer.", "type": "project", "license": "proprietary", "authors": [ { "name": "Narendra Padala", "email": "narendrapadala@gmail.com", "homepage": "http://scripthere.com/" } ], "repositories": [ { "type": "composer", "url": "https://wpackagist.org" } ], "extra": { "wordpress-install-dir": "core", "installer-paths": { "vendor/{$vendor}/{$name}": [ "webdevstudios/cmb2" ], "content/plugins/{$name}/": ["type:wordpress-plugin"], "content/mu-plugins/{$name}": ["type:wordpress-muplugin"], "content/themes/{$name}/": ["type:wordpress-theme"] } }, "scripts": { "post-install-cmd": [ "cp core/index.php ./index.php", "cp core/wp-config-sample.php ./wp-config.php" ] }, "require": { "composer/installers": "^v1.3", "johnpbloch/wordpress": "^4.9" }, "require-dev": { "wpackagist-plugin/whats-running": "^1.9", "wpackagist-plugin/debug-wp-redirect": "^1.1", "wpackagist-plugin/post-meta-inspector": "^1.1", "wpackagist-plugin/post-meta-manager": "^1.0", "wpackagist-plugin/debug-meta-data": "^1.0", "wpackagist-theme/twentyseventeen": "^1.4" } } |
3.Next, open a terminal and go application path and run composer install a couple of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
narendra@narendra-Aspire-E1-571:/var/www/html/scripthere$ composer install Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 10 installs, 0 updates, 0 removals - Installing composer/installers (v1.5.0): Loading from cache - Installing johnpbloch/wordpress-core-installer (1.0.0.2): Loading from cache - Installing wpackagist-plugin/debug-wp-redirect (1.1): Loading from cache - Installing wpackagist-theme/twentyseventeen (1.4): Loading from cache - Installing johnpbloch/wordpress-core (4.9.4): Loading from cache - Installing johnpbloch/wordpress (4.9.4): Loading from cache - Installing wpackagist-plugin/whats-running (1.9.1): Loading from cache - Installing wpackagist-plugin/post-meta-inspector (1.1.1): Loading from cache - Installing wpackagist-plugin/post-meta-manager (1.0.4): Loading from cache - Installing wpackagist-plugin/debug-meta-data (1.0.3): Loading from cache Writing lock file Generating autoload files narendra@narendra-Aspire-E1-571:/var/www/html/scripthere$ composer install Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Nothing to install or update Generating autoload files > cp core/index.php ./index.php > cp core/wp-config-sample.php ./wp-config.php |
4.Next, open the index.php file of the application and set the WordPress “core” path.
1 2 3 4 5 6 7 8 9 |
/** * Tells WordPress to load the WordPress theme and output it. * * @var bool */ define('WP_USE_THEMES', true); /** Loads the WordPress Environment and Template */ require( dirname( __FILE__ ) . '/core/wp-blog-header.php' ); |
5.Next, open the “wp-config.php” file of the application and make settings such as database, vendor, content path, plugin, theme path etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'your db name'); /** MySQL database username */ define('DB_USER', 'your user name'); /** MySQL database password */ define('DB_PASSWORD', 'your password'); /** MySQL hostname */ define('DB_HOST', 'localhost'); /** Database Charset to use in creating database tables. */ define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', ''); /** * Composer's autoloader, used throughout the project */ require_once( __DIR__ . '/vendor/autoload.php' ); /** * Dynamic home and site URLs to handle both websites in all environments, set here your folder name. */ define( 'SITE_NAME', 'scripthere'); /** * Dynamic home and site URLs to handle both websites in all environments. */ define( 'WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] .'/'. SITE_NAME.'/core' ); define( 'WP_HOME', 'http://' . $_SERVER['HTTP_HOST'].'/'.SITE_NAME); /** * Custom plugins directory shared across both websites. */ define( 'WP_PLUGIN_DIR', dirname( __FILE__ ) . '/content/plugins' ); define( 'WP_PLUGIN_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/content/plugins' ); /** * Custom must-use plugins directory shared across both websites. */ define ( 'WPMU_PLUGIN_DIR', dirname ( __FILE__ ) . '/content/mu-plugins' ); define ( 'WPMU_PLUGIN_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/content/mu-plugins' ); /** * Custom content directory per site based on defined constant. */ define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/content' ); define( 'WP_CONTENT_URL', 'http://' . $_SERVER['HTTP_HOST'] .'/'.SITE_NAME. '/content' ); |
6.Next, go to browser and start the installation with your application url , example @http://localhost/scripthere/
7.Next, login as admin the application and set permalink settings as “/%postname%/” and save, @http://localhost/scripthere/core/wp-admin/options-permalink.php
8.That’s all your done, now you can able browse the site in the root (i.e @http: //localhost/scripthere/) even installed WordPress core in a subdirectory, make sure when you’re going to access admin we need to include the “core” in the url (i.e @http: //localhost/scripthere/core/wp-login.php).
Hopefully, this article gives you a great idea about the different directory structures, allowing you to enable another variation separated by the installation. Please Subscribe to ScriptHere.Com by Email. You can also find us on Facebook and Twitter. What is your beloved WordPress installation setup ? Please tell me the following comments.