A simple dev container template for WordPress plugin development.
Important
Strongly recommend: if you use Linux, your uid
should be 1000
. Else docker may cause permission problems.
-
Install requirements:
- Visual Studio Code
- Dev Containers extension
- Docker environment (Linux PC is recommend)
Remote Development:
- Prepare Linux PC. Recommended RAM is 4GB or higher.
- Install requirements in your terminal PC:
- Configure server.
- Install docker.
- Setup Remote Tunnels or SSH (configure your router for forwarding ssh port).
- Setup WoL or remote power-on method, if you need.
- Connect to server.
- Recommended: Install Docker for Visual Studio Code in server.
-
Clone this repository.
git clone https://github.com/chrono-meter/wp-plugin-devcontainer.git YOUR-PROJECT-NAME
-
Open cloned folder named
YOUR-PROJECT-NAME
in Visual Studio Code. -
Edit devcontainer-docker-compose-options.yml, and edit test-docker-compose-options.yml.
-
Open dev container:
Ctrl+Shift+P
(Command pallete) →Dev Containers: Open Folder in Container...
. -
Open WordPress via forward port:
Ctrl+Shift+P
(Command pallete) →Forward a Ports
→WordPress (80)
→ open link inForwarded Address
column -
Login into WordPress. Default username is
admin
, password ispassword
(If not set by environment variable).
Dev container is inherited from WordPress official image.
This container execute following steps on startup:
-
Build image. See .devcontainer/wordpress-base.Dockerfile.
- Configure user settings for www-data console operation.
- Configure "php.ini".
- Install composer.
- Install WP-CLI.
wp core download
like operation.- Set default user as
www-data
. - Set default directory as
/var/www/html
. - Install Xdebug.
- Install
phpinfo()
file, location ishttp://localhost:80/phpinfo.php
. - Install Adminer, location is
http://localhost:80/adminer.php
.
-
Wait for MySQL ready.
-
Configure
wp-config.php
. -
wp core install
with environment variables. -
Run shell script file for WordPress first installation. Set
$WORDPRESS_SETUP_SCRIPT
environment variable.
Dev container's smtp environment is provided by Mailhog.
To see emails that sent from WordPress, Ctrl+Shift+P
(Command pallet) → Forward a Ports
→ Find forwarded 8025
port and open it in your browser.
To check emails in your test, use MailHog API v1 or MailHog API v2.
- WordPress Image
- .devcontainer/devcontainer.json
devcontainer
service in .devcontainer/common.yml- .devcontainer/devcontainer-docker-compose.yml
- Enter dev container console (shell):
Shift+Esc
orCtrl+Shift+@
(new console) - Forward a Ports:
Ctrl+Shift+P
(Command pallet) →Forward a Ports
- Exit/Reopen/Rebuild dev container:
Ctrl+Shift+P
(Command pallet) → typereopen
- Select and run task:
Shift+Alt+F10
→ Select task - Run test:
Ctrl+Shift+P
(Command pallet) →Tasks: Run Test Task
- Run build tasks:
Ctrl+F9
- Find your favorite key bindings.
- Reload apache (if you don't want to restart dev container): Run
/etc/init.d/apache2 reload
in dev container's console
- Bookmark
javascript:(function() {document.cookie='XDEBUG_TRIGGER='+'VSCODE'+';path=/;';document.location.reload();setTimeout(function() {document.cookie='XDEBUG_TRIGGER='+''+';expires=Mon, 05 Jul 2000 00:00:00 GMT;path=/;';}, 100);})()
- Set breakpoint.
- Start Xdebug:
Shift+Alt+F9
orRun and Debug
in Left sidebar →Listen for Xdebug
- Open WordPress page that you want to debug.
- Open bookmarked JavaScript.
Do you get frustrated when your breakpoints don't work in Xdebug? First, calm down, before you waste your precious time.
- Install browser extension, then enable on your site.
- Unfortunately, the breakpoint that you set will be never reached on your request or execute condition. Set new breakpoint on prior line or upper function calling.
- Do
ping `php -r 'echo ini_get("xdebug.client_host");'`
to check Xdebug client is reachable. - Finally, try to call
\xdebug_break();
on the breakpoint. If this method doesn't work then your Xdebug configuration is broken.
User Role Editor activation failure via wp-cli installation.
There are lacking caps:
- ure_create_roles
- ure_delete_roles
- ure_create_capabilities
- ure_delete_capabilities
- ure_manage_options # URE_KEY_CAPABILITY
- ure_reset_roles
So, we need to activate it manually:
wp plugin install --activate user-role-editor
wp eval 'do_action("activate_user-role-editor/user-role-editor.php");'