Closed jcvignoli closed 9 months ago
Thanks for the suggestion, @jcvignoli !
This is achievable with a little bit of custom code:
$ cat hook.php
<?php
WP_CLI::add_hook(
'before_run_command',
function( $command ) {
$base = array_shift( $command );
if ( 'plugin' === $base ) {
WP_CLI::log( 'You ran the plugin command!' );
}
}
);
$ wp --require=hook.php plugin status hello
You ran the plugin command!
Plugin hello details:
Name: Hello Dolly
Status: Inactive
Version: 1.7.2
Author: Matt Mullenweg
$ wp --require=hook.php theme status twentytwentyfour
Theme twentytwentyfour details:
Name: Twenty Twenty-Four
Status: Inactive
Version: 1.0
Author: the WordPress team
@danielbachhuber, this doesn't work. I tried the following code:
WP_CLI::add_hook(
'before_run_command',
function( $command ) {
define('MY_CONST_THAT_CHANGE_DB', true);
WP_CLI::log( 'You ran the plugin command!' );
}
);
A wp config list
(with the --require) shows me it swittched to the correct database. However, a wp cron run event --due-now
sticks to the default database, the one that is selected if the constant is not found.
The constant is not passed when using this method.
However, a
wp cron run event --due-now
sticks to the default database, the one that is selected if the constant is not found. The constant is not passed when using this method.
@jcvignoli Is the constant already defined?
@danielbachhuber No, it's only generated in the file added in the --require command. I don't get any PHP message saying the const is already defined.
For the sake of debugging, I launched a cron command with adding in my wp-config file a
print_r( MY_CONST_THAT_CHANGE_DB );
and got that
PHP Fatal error: Uncaught Error: Undefined constant "MY_CONST_THAT_CHANGE_DB" in /home/www/mywordpressinstall/wp-config.php:10
which is the line where print_r( MY_CONST_THAT_CHANGE_DB );
was added.
@danielbachhuber could you reopen this very issue? Until a solution is found, there is no way afaik to pass a constant with a cron.
Until a solution is found, there is no way afaik to pass a constant with a cron.
This is a support request though, not a bug with WP-CLI. The #cli
channel on WordPress.org Slack is a good place to ask support questions.
I think, but I'm not positive, that cron might be spawning a HTTP request. If you define( 'DISABLE_WP_CRON', true );
, you can run cron entirely with WP-CLI.
It may be useful to use the argument
--exec=<php-code>
when executingwp plugin whatever
If using on the same local dev two different databases, one can't access both. But sending for example a
define( 'FIRST_DB', true )
would allow to switch from one db to the second.