ddev icon indicating copy to clipboard operation
ddev copied to clipboard

Setting WordPress config file with post-start hook

Open Ebeldev opened this issue 3 years ago • 10 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Run a Diagnostic and Paste Link Here

No response

Current Behavior

I have set a config.custom.yaml file.

hooks: post-start: - exec: "wp config set WP_DEBUG_LOG true" - exec: "wp config set WP_DEBUG_DISPLAY false" - exec: "wp config set display_errors 0" - exec: "wp config set SCRIPT_DEBUG true" - exec: "wp config set custom_ENV production"

Here is the error. Error: Could not process the 'wp-config.php' transformation. Reason: Unable to locate placement anchor. Task failed: Exec command 'wp config set SCRIPT_DEBUG true' in container/service 'web': exit status 1

Expected Behavior

I expect that theses configs settings gets added to the config of my projet.

Steps To Reproduce

No response

Anything else?

No response

Ebeldev avatar Jun 27 '22 15:06 Ebeldev

Are you asking for DDEV to do more? Or are you asking why wp config set SCRIPT_DEBUG true fails? If you're asking why that fails, is that something that should be filed in the wp-cli issue queue?

rfay avatar Jun 27 '22 17:06 rfay

@rfay Hi Randy, thanks for your reply. ..

Well I just want to set the WP Debug correctly when DDev start.

As well I wanted to to know if that's the correct way of setting custom constants.

I'll reach out to wp cli to see if they can see why I cannot set config fails. But I suspect they will tell me to look with DDEV.

Ebeldev avatar Jun 27 '22 18:06 Ebeldev

Experiment with just using the wp command inside the web container. Try each of these. Try it with a plain vanilla WP checkout. You'll be able to sort out what's going on. That's the first thing to do - move back from trying hooks to just running the command directly in the web container (ddev ssh).

rfay avatar Jun 27 '22 19:06 rfay

I did try from ddev ssh

ddev ssh
wp config set WP_DEBUG_LOG true Error: Could not process the 'wp-config.php' transformation. Reason: Unable to locate placement anchor.

or

wp config set --raw WP_DEBUG_LOG true Error: Could not process the 'wp-config.php' transformation. Reason: Unable to locate placement anchor.

Outside of the container. Just in the terminal where my files are. I do get the same error.

wp config set WP_DEBUG_LOG true Error: Could not process the 'wp-config.php' transformation. Reason: Unable to locate placement anchor.

or

wp config set --raw WP_DEBUG_LOG true Error: Could not process the 'wp-config.php' transformation. Reason: Unable to locate placement anchor.

From my reseach https://github.com/roots/bedrock/issues/544, it seems that WP CLI does look for the "/* That's all, stop editing!". And if WP CLI does not found that in th config file it won't set configurations.

And since DDev does a special config file (wp-config-ddev.php), I do not know if wp cli does understand that when you do use the wp cli it goes in the right file.

Ebeldev avatar Jun 30 '22 04:06 Ebeldev

I just added this to the config file : /* That's all, stop editing!

and run directly in my terminal not even in ddev ssh. wp config set WP_DEBUG_LOG true And it did work.

Screen Shot 2022-06-30 at 12 15 17 AM

If I move the "/* That's all, stop editing! */" in the wp-config-ddev file, then wp cli set config command does not work.

So what I see is that the wp-config file that is generated by ddev should include this line : "/* That's all, stop editing! */"

Once that runs, then the wp set config command will be able to run in the pre-start hook.

Otherwise, everytime I ddev start I have to go and reset different config I have. Specially when I'm debugging.

Ebeldev avatar Jun 30 '22 04:06 Ebeldev

But using wp config set with the pre-start hook does not work.

If the special line is added in the config file then all other config will be written in the config file not in the ddev-config file.

Ebeldev avatar Jun 30 '22 04:06 Ebeldev

I try using the --anchor argument it does work.

But It would need a special comment like : //customconfigsettings in the wp-config file that is generated by ddev.

Then use : wp config set WP_DEBUG_LOG true --anchor=//customconfigsettings

Then that would work too.

Screen Shot 2022-06-30 at 12 34 04 AM

Personnaly, I think it would be better to keep the original string from WordPress config file that wp cli is looking for.

Ebeldev avatar Jun 30 '22 04:06 Ebeldev

A PR would be very welcome if it solves the situation for you! I assume it's https://github.com/drud/ddev/blob/a9a72be380e1d850251c61aa1897b4641db5eb23/pkg/ddevapp/wordpress/wp-config.php or https://github.com/drud/ddev/blob/a9a72be380e1d850251c61aa1897b4641db5eb23/pkg/ddevapp/wordpress/wp-config-ddev.php that you would change?

rfay avatar Jun 30 '22 15:06 rfay

@Ebeldev since you seem to completely understand this now, could you please update the original post and the title to reflect what you learned? A PR is welcome, and seems easy. But it does seem like this is an inflexibility in the wp-cli?

rfay avatar Jul 02 '22 13:07 rfay

Interesting to learn that WP-CLI looks for /* That's all, stop editing! Happy publishing. */. But it makes sense. Guess this is also implemented in WP core and plugins. I learn so much here. :-D :-o

We discussed in another issue that we don't use the official wp-config-sample.php in DDEV yet, which includes this anchor in line 88 by default:

https://github.com/WordPress/WordPress/blob/master/wp-config-sample.php#L88

By modifying https://github.com/drud/ddev/blob/a9a72be380e1d850251c61aa1897b4641db5eb23/pkg/ddevapp/wordpress/wp-config.php to match official wp-config-sample.php we should get rid of the error and be more standard-compliant as well.

Nice catch @Ebeldev! If my time allows it I could provide help testing a PR! Cheers!

mandrasch avatar Jul 02 '22 19:07 mandrasch

I'm fine with a PR to deal with this, it doesn't seem like it's actually something that relates to DDEV at this point, but I'm sure open to contributions. Thanks for sorting it out!

rfay avatar Aug 27 '22 00:08 rfay

@rfay or @mandrasch Was this put in a pr request ? What this solved ?

Ebeldev avatar Oct 13 '22 13:10 Ebeldev

See https://github.com/drud/ddev/issues/3940#issuecomment-1172900001 - We were waiting for a PR from you @Ebeldev ... or anybody :)

rfay avatar Oct 13 '22 13:10 rfay

I created a PR #5434 to hopefully address this issue. It's my first PR on this repo. I hope I did it correctly.

Meanwhile, until/unless that PR is accepted, here's a hacky method I'm using in config.yaml to insert the needed comment:

hooks:
  post-start:
    - exec-host: |
        grep -q "/\* That's all, stop editing! Happy publishing. \*/" $DDEV_DOCROOT/wp-config.php || \
        (grep -q "/\*\* Absolute path to the WordPress directory. \*/" $DDEV_DOCROOT/wp-config.php && \
        sed -i "" "s/^\/\*\* Absolute path to the WordPress directory. \*\//\/* Add any custom values between this line and the \"stop editing\" line. *\/\n\n\n\n\/* That's all, stop editing! Happy publishing. *\/\n\n&/" $DDEV_DOCROOT/wp-config.php)
    - exec: wp config set <whatever>

The script looks for the existence of the needed line /* That's all, stop editing! Happy publishing. */ in wp-config.php. If it doesn't find it, it looks for the line /** Absolute path to the WordPress directory. */ and adds the following default wordpress block before it:

/* Add any custom values between this line and the "stop editing" line. */



/* That's all, stop editing! Happy publishing. */

After that edit's been made, subsequent- exec: wp config set commands in config.yaml are executed successfully. And CLI ddev wp config set commands also work correctly.

paulrudy avatar Oct 15 '23 07:10 paulrudy

just an update, this was fixed with #5434 and merged in v1.22.4

paulrudy avatar Oct 31 '23 05:10 paulrudy

Congrats and thanks @paulrudy !

rfay avatar Oct 31 '23 12:10 rfay