---
title: Laravel Homestead for WordPress Theme and Plugin Development
date: 2018-12-09T12:49:49+00:00
modified: 2018-12-09T12:53:09+00:00
image:: https://kaspars.net/wp-content/uploads/2018/12/guillaume-briard-776177-unsplash.jpg
permalink: https://kaspars.net/blog/laravel-homestead-wordpress
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
category:
  - Development
  - WordPress
post_tag:
  - PHP
---

# Laravel Homestead for WordPress Theme and Plugin Development

Turns out [Laravel Homestead](https://laravel.com/docs/5.7/homestead) is almost exactly [the development environment I was looking for](https://kaspars.net/blog/wordpress/wordpress-environment-composer) — it [can be added as a Composer dependancy](https://laravel.com/docs/5.7/homestead#per-project-installation) to any PHP project and configured using [a simple Yaml file](https://laravel.com/docs/5.7/homestead#configuring-homestead). The host machine needs only [Vagrant](https://www.vagrantup.com/) and [VirtualBox](https://www.virtualbox.org/).

**tl;dr:** See [this pull request](https://github.com/kasparsd/widget-context-wporg/pull/48) for how I added Homestead to the [Widget Context plugin](https://widgetcontext.com/).

## How it Works

First, we add a very minimal `Vagrantfile` to the project root which reads the Homestead’s configuration from `Homestead.yaml` (could be named anything) and triggers the provisioning logic in `<a href="https://github.com/laravel/homestead/blob/4d29510c8a08551886e60a59430149877f9bd0e1/scripts/homestead.rb">scripts/homestead.rb</a>` using the supplied configuration.

We install WordPress a development dependancy in `package.json` and configured it from the same `Vagrantfile` using an inline shell script (and [WP-CLI](https://wp-cli.org/) which comes bundled with Homestead):

```
config.vm.provision "shell",
	inline: "wp config create",
	privileged: false
```

 and use a dedicated `wp-cli.yaml` which defines the database access parameters and credentials:

```
path: /home/vagrant/code

config create:
  dbname: homestead
  dbuser: homestead
  dbpass: secret
```

which are used as defaults during `wp config create`.

Note that `wp-cli.yaml` lives within our theme directory so we specify the `WP_CLI_CONFIG_PATH` environment variable in `Homestead.yaml` which points to `wp-cli.yaml` inside the virtual environment.

## Notes

Laravel Homestead runs the provision scripts as `root` inside the virtual machine so the regular non-privileged `vagrant` user can’t write to disk which prevents us from downloading and setting up WordPress from within the virtual environment. This can probably be adjusted with a few additional lines of configuration in `Homestead.yaml`.