Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp plugin activate should show what unexpected output the plugin generated #329

Open
Luc45 opened this issue Oct 5, 2022 · 1 comment
Open

Comments

@Luc45
Copy link

Luc45 commented Oct 5, 2022

If a plugin generates output during activation, activate_plugin will return an WP_Error object configured like this:

$output = ob_get_clean();
return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );

As you can see, the output is passed as the third parameter, and the error has code "unexpected_output".

I have a use-case where I need the output that was generated for logging reasons.

Relevant code that should print the output:

$result = activate_plugin( $plugin->file, '', $network_wide );
if ( is_wp_error( $result ) ) {
$message = $result->get_error_message();
$message = preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
$message = wp_strip_all_tags( $message );
$message = str_replace( 'Error: ', '', $message );
WP_CLI::warning( "Failed to activate plugin. {$message}" );

@danielbachhuber
Copy link
Member

Hey @Luc45,

Thanks for the suggestion!

I have a use-case where I need the output that was generated for logging reasons.

I'm a little hesitant about changing the output solely for this use case.

What do you think about instead running the $result through an action, so your custom code can capture and report the output however you need?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants