sqlite-database-integration icon indicating copy to clipboard operation
sqlite-database-integration copied to clipboard

Do not redirect when activating plugin via WP-CLI

Open swissspidy opened this issue 2 years ago • 2 comments

We‘d like to run WP-CLI tests with SQLite using this plugin. I noticed this issue during one of the test runs, as the attempted redirect can cause warnings in CLI. I suggest not doing a redirect if the WP_CLI constant is defined and true.

swissspidy avatar Oct 21 '23 21:10 swissspidy

  • I think plugins redirecting on activation is obnoxious, so much that I wrote a plugin which prevents it: bh-wp-plugins-page (wouldn't actually work in this case, but this has given me ideas to improve it)
  • There is a code pattern that allows filtering redirects but it was not implemented here, I have opened a PR to add it
  • Since wp_redirect() is implemented in pluggable.php, it might be broadly useful for WP CLI to implement it itself

This should also work:

function prevent_sqlite_plugin_activation_redirect( $plugin ) {
	if ( defined( 'SQLITE_MAIN_FILE' ) && plugin_basename( SQLITE_MAIN_FILE ) === $plugin ) {
		remove_all_actions( 'activated_plugin' );
	}
}
add_action( 'activated_plugin', 'prevent_sqlite_plugin_activation_redirect', 0 );

I've never written custom Behat commands/extensions, so excuse me if I'm off the mark. I'm guessing if the Behat process were to define wp_redirect() and WordPress is part of the same process, it should work. I'm not sure if it's possible for Behat to use add_action() etc.

BrianHenryIE avatar Oct 21 '23 22:10 BrianHenryIE

WP-CLI already does all of that and gives a nice warning because of that. That‘s not the issue. I just think preventing the warning in the first place would be nice, and doing so properly means doing it in the plugin.

swissspidy avatar Oct 22 '23 07:10 swissspidy