bugsnag / bugsnag-wordpress

BugSnag error monitoring for WordPress sites
http://wordpress.org/plugins/bugsnag/
GNU General Public License v2.0
20 stars 15 forks source link

Add ability to define API Key as Constant [REQUEST] #18

Closed chrisatomix closed 5 years ago

chrisatomix commented 7 years ago

Hi, Due to the way we work on our sites across multiple environments (3-6 Development servers, plus Staging and Production, etc), we occasionally have issues with the Bugsnag plugin being enabled on the wrong environment. For example, a Developer brings in the database from the Production site to make local changes, and this causes his Bugsnag installation to start reporting bugs.

To get around this we added the following method to the Bugsnag class in the plugin:

private function apiKey()
{
  return defined('BUGSNAG_API_KEY') ? BUGSNAG_API_KEY : get_option('bugsnag_api_key');
}

If the BUGSNAG_API_KEY constant has been defined, it will use that. Otherwise it falls back to the key defined in the database (which we usually leave blank).

We can then add code like this in our configuration:

if(WP_ENV !== 'development') {
  // Only log Bugsnag errors on Staging/Production
  define('BUGSNAG_API_KEY', '(OUR_API_KEY)');
}

Is there any chance of a similar feature being added to this plugin? We're more than happy to contribute a PR to save us from having to maintain our own fork, with some guidance from the plugin maintainers. Our current fork is out of date and doesn't include all the new network site options, so this could cause some issues if incorrectly implemented.

Thanks in advance

chrisatomix commented 7 years ago

Just a quick update, here's what we added to the end of the bugsnag.php plugin file to enable this in the latest version 1.3.0:

function bugsnag_define_api_key() {
  return defined('BUGSNAG_API_KEY') ? BUGSNAG_API_KEY : false;
}
add_filter('pre_option_bugsnag_api_key', 'bugsnag_define_api_key');
add_filter('pre_site_option_bugsnag_api_key', 'bugsnag_define_api_key');

$bugsnagWordpress = new Bugsnag_Wordpress();
kattrali commented 7 years ago

Thanks for the detailed report, @chrisatomix. I think this would be a good addition, as it sounds like a useful behavior and not unlike what happens in other bugsnag PHP libraries.

tomlongridge commented 5 years ago

Available in v1.4.0.