Skip to content

Commit a721f5a

Browse files
authored
Improving WordPress support (#57)
* Adding WordPress config additions (based on a similar approach by ddev-redis addon) * Replace the actual config file with the temp file * Replace the actual config file with the temp file * Include addon files in the project * Add ${DDEV_APPROOT} to shell script. Added some additional notes to README. * Added some wp-config notes to the README * Revert formatting changes to README * Changed approach so the script modified wp-config.php instead of wp-config-ddev.php * Missing " mark * Improving the removal process to do a better job of cleaning up wp-config.php
1 parent 5f52127 commit a721f5a

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Once Browsersync is running, visit `https://<project>.ddev.site:3000` or run `dd
5858
4. Adds a `.ddev/docker-compose.browsersync.yaml`, which exposes and routes the ports necessary.
5959
5. Adds a `ddev browsersync` shell command, which lets you easily start Browsersync when you want it.
6060

61+
For WordPress projects, this add-on also:
62+
* Adds a `wp-config-ddev-browser.php` file which modifies the WP_HOME and WP_SITEURL values to work with Browsersync.
63+
* On install, modifies the `wp-config-ddev.php` file to include the `wp-config-ddev-browser.php` file.
64+
6165
## Other ways to use browsersync with this add-on
6266

6367
There are many other options to integrate browsersync into your project, including:
@@ -128,3 +132,34 @@ ddev exec npm run watch
128132
- Browsersync will be running on **HTTPS** at `https://browsersync-demo.ddev.site:3000`
129133

130134
**Contributed and maintained by [tyler36](https://github.com/tyler36)**
135+
136+
### WordPress Configuration Changes.
137+
138+
The changes this add-on makes to the `wp-config-ddev.php` file during installation can be seen below.
139+
140+
The `wp-config-ddev-browserync.php` file is included before the `/** WP_HOME URL */` comment.
141+
142+
Before:
143+
144+
```php
145+
/** WP_HOME URL */
146+
defined( 'WP_HOME' ) || define( 'WP_HOME', 'https://projectname.ddev.site' );
147+
148+
/** WP_SITEURL location */
149+
defined( 'WP_SITEURL' ) || define( 'WP_SITEURL', WP_HOME . '/' );
150+
```
151+
152+
After:
153+
154+
```php
155+
/** Include WP_HOME and WP_SITEURL settings required for Browsersync. */
156+
if ( ( file_exists( __DIR__ . '/wp-config-ddev-browsersync.php' ) ) ) {
157+
include __DIR__ . '/wp-config-ddev-browsersync.php';
158+
}
159+
160+
/** WP_HOME URL */
161+
defined( 'WP_HOME' ) || define( 'WP_HOME', 'https://projectname.ddev.site' );
162+
163+
/** WP_SITEURL location */
164+
defined( 'WP_SITEURL' ) || define( 'WP_SITEURL', WP_HOME . '/' );
165+
```

install.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ project_files:
1313
- web-build/Dockerfile.ddev-browsersync
1414
- browser-sync.js
1515
- commands/web/browsersync
16-
16+
- scripts/wp-config-ddev-browsersync.php
17+
- scripts/remove-wordpress-settings.sh
18+
- scripts/setup-wordpress-settings.sh
1719

1820
post_install_actions:
1921
- |
@@ -25,3 +27,12 @@ post_install_actions:
2527
#ddev-nodisplay
2628
#ddev-description:Remove old 'docker-compose.browsersync.yaml'
2729
if grep "#ddev-generated" $DDEV_APPROOT/.ddev/docker-compose.browsersync.yaml 2>/dev/null; then rm -f "$DDEV_APPROOT/.ddev/docker-compose.browsersync.yaml"; fi
30+
#ddev-description:Install browsersync settings for WordPress if applicable
31+
scripts/setup-wordpress-settings.sh
32+
33+
removal_actions:
34+
- |
35+
#ddev-nodisplay
36+
#ddev-description:Remove browsersync settings for WordPress if applicable
37+
rm -f "${DDEV_APPROOT}/wp-config-ddev-browsersync.php"
38+
scripts/remove-wordpress-settings.sh
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
#ddev-generated
3+
set -e
4+
5+
if [[ $DDEV_PROJECT_TYPE != wordpress ]] ;
6+
then
7+
exit 0
8+
fi
9+
10+
if ( ddev debug configyaml 2>/dev/null | grep 'disable_settings_management:\s*true' >/dev/null 2>&1 ) ; then
11+
exit 0
12+
fi
13+
14+
SETTINGS_FILE_NAME="${DDEV_APPROOT}/wp-config.php"
15+
16+
echo "Removing wp-config-ddev-browsersync.php from: ${SETTINGS_FILE_NAME}"
17+
18+
# Remove the ddev-browsersync config that we added.
19+
awk '
20+
/\/\*\* Include for ddev-browsersync to modify WP_HOME and WP_SITEURL\./ { skip=1 }
21+
skip && /\}.*$/ { skip=0; getline; next }
22+
!skip
23+
' ${DDEV_APPROOT}/wp-config.php > ${DDEV_APPROOT}/wp-config-temp.php
24+
mv ${DDEV_APPROOT}/wp-config-temp.php ${DDEV_APPROOT}/wp-config.php
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
#ddev-generated
3+
set -e
4+
5+
if [[ $DDEV_PROJECT_TYPE != wordpress ]] ;
6+
then
7+
exit 0
8+
fi
9+
10+
if ( ddev debug configyaml 2>/dev/null | grep 'disable_settings_management:\s*true' >/dev/null 2>&1 ) ; then
11+
exit 0
12+
fi
13+
14+
cp scripts/wp-config-ddev-browsersync.php $DDEV_APPROOT/
15+
16+
SETTINGS_FILE_NAME="${DDEV_APPROOT}/wp-config.php"
17+
18+
echo "Settings file name: ${SETTINGS_FILE_NAME}"
19+
20+
# If wp-config.php already contains the require_once() then exit early.
21+
if grep -q "/\*\* Include for ddev-browsersync to modify WP_HOME and WP_SITEURL. \*/" ${DDEV_APPROOT}/wp-config.php; then
22+
exit 0
23+
fi
24+
25+
echo "Adding wp-config-ddev-browsersync.php to: ${SETTINGS_FILE_NAME}"
26+
27+
# Append our code before the ddev config comment.
28+
awk '
29+
/\/\/ Include for ddev-managed settings in wp-config-ddev.php./ {
30+
print "/** Include for ddev-browsersync to modify WP_HOME and WP_SITEURL. */"
31+
print "$ddev_browsersync_settings = dirname( __FILE__ ) . \"/wp-config-ddev-browsersync.php\";"
32+
print ""
33+
print "if ( is_readable( $ddev_browsersync_settings ) ) {"
34+
print " require_once( $ddev_browsersync_settings );"
35+
print "}"
36+
print ""
37+
print "// Include for ddev-managed settings in wp-config-ddev.php."
38+
next
39+
}
40+
{print}
41+
' ${DDEV_APPROOT}/wp-config.php > ${DDEV_APPROOT}/wp-config-temp.php
42+
43+
# Replace the real config file with the modified version in temporary file.
44+
mv ${DDEV_APPROOT}/wp-config-temp.php ${DDEV_APPROOT}/wp-config.php
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
// #ddev-generated
3+
/** Allow any domain/port so WordPress allows browsersync's :3000 URLs */
4+
if ( ! empty( $_SERVER['SERVER_PORT'] ) && ! empty( $_SERVER['SERVER_NAME'] ) ) {
5+
// phpcs:ignore
6+
$domain = sprintf( '%s://%s', $_SERVER['SERVER_PORT'] == 80 ? 'http' : 'https', $_SERVER['SERVER_NAME'] );
7+
8+
/** WP_HOME URL */
9+
define( 'WP_HOME', $domain );
10+
11+
/** WP_SITEURL location */
12+
define( 'WP_SITEURL', $domain );
13+
}

0 commit comments

Comments
 (0)