• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Install WordPress with Composer

Install WordPress with Composer

Last updated on December 27, 2018 by Sal Ferrarello

You can setup WordPress in a sub-directory using composer, thanks to John P Bloch‘s WordPress Core Installer. Below I’ve outlined the bare minimum steps to get this running.

Step 1: composer.json

Create a composer.json in the root of your project with the following content.

{
  "require": {
    "johnpbloch/wordpress": "*"
  }
}

Step 2: Execute composer

If you don’t have composer installed already, install composer. Then run the following from the command line at the root of your project.

composer install

At this point you should have a wordpress directory with your core files.

Step 3: Copy index.php

Copy wordpress/index.php to index.php. You can do this from the command line with

cp wordpress/index.php ./index.php

Step 4: Modify index.php in your project root

The index.php in the root of your project (the one you just created via a copy command) should be modified.

require( dirname( __FILE__ ) . '/wp-blog-header.php' );

becomes

require( dirname( __FILE__ ) . '/wordpress/wp-blog-header.php' );

Step 5: Create wp-config.php

Create wp-config.php (and the database if it does not yet exist). Add the following lines to wp-config.php. These lines keep wp-content and its contents in the default installation location even though the core files are loaded from the /wordpress directory.

define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/wp-content' );
define ('WP_CONTENT_URL', 'http://wpsubdir.dev' . '/wp-content' );

Step 6: Set up WordPress

Visit your website and setup the database and your initial user (this is the same procedure for any WordPress website).

Step 7: Adjust the Site Address

At this point, your WordPress site should be setup in your sub-directory (e.g. http://wpsubdir.dev/wordpress/). However, generally we want the site to load in the top level directory (e.g. http://wpsubdir.dev/).

We make this change at Settings > General (i.e. /wordpress/wp-admin/options-general.php)
and remove “/wordpress” from the Site Address (URL)

WordPress Core Sub-directory Settings

Full Disclosure: I don’t use this technique

Typically, I do not install WordPress in a sub-directory. I use Local by Flywheel for running sites locally and use my WordPress .gitignore to keep the WordPress core files out of my repo. On the projects I work on where WordPress is installed in a sub-directory, we are using WP Starter.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Dev Tips, Programming Tagged With: composer, WordPress

Reader Interactions

Comments

  1. a says

    June 28, 2020 at 4:52 pm

    john ploch… oki. who is he?

    Reply
    • Doug says

      May 4, 2021 at 8:33 am

      Google is your friend. https://github.com/johnpbloch

      Reply
  2. Nickolay says

    September 8, 2021 at 5:36 am

    I get HTTP ERROR 500 on Step 6

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in