It’s going to happen one day. You’re going to get that weird error when you activate your plugin to test it and you will have no idea what it means.
What error?
Unexpected output.
The problem is WordPress doesn’t tell you what did it, where, or why, not even if you have debug turned on. You do have debug turned on, right? Well I was stumped on this one. I checked all my PHP calls to make sure I didn’t have whitespace, I looked for any files that were accidentally saved UTF-8, and I checked and double checked my diffs between the good and bad versions.
What I ended up doing was writing a function that saved the error and output it.
/* For debugging only */ if (defined('WP_DEBUG') && true === WP_DEBUG) { function myplugin_activated_plugin_error() { update_option( 'myplugin_error', ob_get_contents() ); } function myplugin_deactivated_plugin_error() { delete_option( 'myplugin_error' ); } add_action( 'activated_plugin', 'myplugin_activated_plugin_error' ); add_action( 'deactivated_plugin', 'myplugin_deactivated_plugin_error' ); function myplugin_message_plugin_error() { ?> <div class="notice notice-error"> <p><?php echo get_option( 'myplugin_error' ); ?></p> </div> <?php } if( get_option( 'myplugin_error' ) ) { add_action( 'admin_notices', 'myplugin_message_plugin_error' ); } }
That results in a pretty kind of output:
I like to use the pretty display so I can easily read what, exactly I messed up and how.
Comments
One response to “Debugging Unexpected Output”
Used this to fix a PHP 7 deprecated notice. This is going in my developer must use plugins folder! Thanks π