A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.
This plugin can report PHP errors (optionally) and JavaScript errors (optionally) to Sentry and integrates with its release tracking.
It will auto detect authenticated users and add context where possible. All context/tags can be adjusted using filters mentioned below.
This plugin requires PHP 7.2
+ but urges users to use a PHP version that is not end of life (EOL) and no longer supported. For an up-to-date list of PHP versions that are still supported see: http://php.net/supported-versions.php.
2.1.*
of this plugin will be the last to support PHP 5.3
.2.2.*
of this plugin will be the last to support PHP 5.4
.3.11.*
of this plugin will be the last to support PHP 7.1
.Note: Version 5.x
is the most recent version of the wp-sentry plugin and only supports PHP 7.2
and up. If you need PHP 5.4-7.1
support check out version 2.x
or 3.x
but do keep in mind there are a lot of differences in the Sentry PHP SDK used.
2.x
of the wp-sentry plugin uses the 1.x
version of the official Sentry PHP SDK.3.x
of the wp-sentry plugin uses the 2.x
version of the official Sentry PHP SDK.4.x
, 5.x
, & 6.x
of the wp-sentry plugin uses the 3.x
version of the official Sentry PHP SDK.7.x
+ of the wp-sentry plugin uses the 4.x
version of the official Sentry PHP SDK.There are a couple of options to start using the plugin.
Note: This plugin does not do anything by default and only has a diagnostic admin interface to test if you have setup the DSN properly and send test events. Setting up the DSN is required.
wp-content/plugins
foldercomposer require stayallive/wp-sentry
in your projectTo start using the plugin first setup the DSN for either the PHP side or the Browser side or both.
All other configuration options are optional but it's advised you read through them to see if any are applicable to you or are thing you'd like to configure.
Note: When configuring constants in your wp-config.php
do this before the That's all, stop editing! Happy publishing.
line, otherwise they won't work!
Sentry uses something called a DSN (read more) to configure the SDK.
WP_SENTRY_PHP_DSN
(PHP)To track PHP errors add this snippet to your wp-config.php
and replace PHP_DSN
with your actual DSN that you find inside Sentry in the project settings under "Client Keys (DSN)":
define( 'WP_SENTRY_PHP_DSN', 'PHP_DSN' );
Note: Do not set this constant to disable the PHP tracker.
Note: This constant was previously called WP_SENTRY_DSN
and is still supported.
WP_SENTRY_BROWSER_DSN
(Browser)To track JavaScript errors add this snippet to your wp-config.php
and replace JS_DSN
with your actual DSN that you find inside Sentry in the project settings under "Client Keys (DSN)":
define( 'WP_SENTRY_BROWSER_DSN', 'JS_DSN' );
// You can _optionally_ enable or disable the JavaScript tracker in certain parts of your site with these constants:
define('WP_SENTRY_BROWSER_ADMIN_ENABLED', true); // Add the JavaScript tracker to the admin area. Default: true
define('WP_SENTRY_BROWSER_LOGIN_ENABLED', true); // Add the JavaScript tracker to the login page. Default: true
define('WP_SENTRY_BROWSER_FRONTEND_ENABLED', true); // Add the JavaScript tracker to the front end. Default: true
Note: Do not set this constant to disable the JavaScript tracker.
Note: This constant was previously called WP_SENTRY_PUBLIC_DSN
and is still supported.
WP_SENTRY_SEND_DEFAULT_PII
If this flag is enabled, certain personally identifiable information is added by active integrations. Without this flag they are never added to the event, to begin with.
If possible, it’s recommended to turn on this feature and use the server side PII stripping to remove the values instead.
When enabled the current logged in user and IP address will be added to the event.
define( 'WP_SENTRY_SEND_DEFAULT_PII', true );
WP_SENTRY_VERSION
Define a version of your site. By default the active theme version will be used or unspecified
if theme version could not be resolved.
This is used for tracking at which version of your site the error occurred. When combined with release tracking this is a very powerful feature.
define( 'WP_SENTRY_VERSION', 'v8.2.0' );
WP_SENTRY_ENV
Define an environment of your site. Defaults to the WordPress environment type from wp_get_environment_type()
or unspecified
if none configured.
This is used for tracking on which environment of your site the error occurred.
define( 'WP_SENTRY_ENV', 'production' );
WP_SENTRY_ERROR_TYPES
(PHP)Set the error types the PHP tracker will track:
define( 'WP_SENTRY_ERROR_TYPES', E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_USER_DEPRECATED & ~E_USER_NOTICE );
Note: You can set any combination of error types you want, see the PHP documentation for more information.
WP_SENTRY_TRACES_SAMPLE_RATE
(PHP)Set the desired sampling rate for performane tracing. Replace 0.3
with your desired sampling rate (0.3
means sample ~30% of your traffic):
// https://docs.sentry.io/platforms/php/performance/#configure
define( 'WP_SENTRY_TRACES_SAMPLE_RATE', 0.3 ); // traces_sample_rate
Enabling tracing will also set SAVEQUERIES
, this can use more memory to save all the executed queries in your environment. If this is not desirable you can disable query tracing by configuring WP_SENTRY_TRACING_FEATURES
.
Note: Do not set this constant or set the sample rate to 0.0
to disable the performance monitoring.
WP_SENTRY_TRACING_FEATURES
(PHP)Enable performance monitoring features by adding this snippet to your wp-config.php
:
define( 'WP_SENTRY_TRACING_FEATURES', [
'db' => [
'spans' => true,
'breadcrumbs' => defined( 'SAVEQUERIES' ) && SAVEQUERIES,
],
'http' => [
'spans' => true,
'breadcrumbs' => true,
],
'transients' => [
'spans' => true,
'breadcrumbs' => true,
],
] );
Note: Not configuring this constant will default to the above configuration.
WP_SENTRY_BROWSER_TRACES_SAMPLE_RATE
(Browser)Enable JavaScript performance tracing by adding this snippet to your wp-config.php
and replace 0.3
with your desired sampling rate (0.3
means sample ~30% of your traffic):
// https://docs.sentry.io/platforms/javascript/performance/#configure-the-sample-rate
define( 'WP_SENTRY_BROWSER_TRACES_SAMPLE_RATE', 0.3 ); // tracesSampleRate
// These options are passed directly to `new BrowserTracing({})`
// define( 'WP_SENTRY_BROWSER_TRACING_OPTIONS', [] );
Note: Do not set this constant or set the sample rate to 0.0
to disable the JavaScript performance tracing.
WP_SENTRY_BROWSER_REPLAYS_SESSION_SAMPLE_RATE
(Browser)Enable JavaScript Session Replay by adding this snippet to your wp-config.php
and replace 0.3
with your desired sampling rate (0.3
means sample ~30% of your traffic):
// These options are injected into the `Sentry.init()` call
// https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
define( 'WP_SENTRY_BROWSER_REPLAYS_SESSION_SAMPLE_RATE', 0.1 ); // replaysSessionSampleRate
define( 'WP_SENTRY_BROWSER_REPLAYS_ON_ERROR_SAMPLE_RATE', 1.0 ); // replaysOnErrorSampleRate
// These options are passed directly to `new Replay({})`
// - https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
// - https://docs.sentry.io/platforms/javascript/session-replay/privacy/#privacy-configuration
// define( 'WP_SENTRY_BROWSER_SESSION_REPLAY_OPTIONS', [ 'maskAllText' => true ] );
Note: Do not set these constants or set the sample rates to 0.0
to disable the JavaScript Session Replay.
WP_SENTRY_PROFILES_SAMPLE_RATE
(PHP)Note: This feature requires the Excimer PHP extension to be installed. See the Sentry PHP SDK documentation for more information.
Sentry's tracing has to be enabled in order for profiling to work. So you also need to configure WP_SENTRY_TRACES_SAMPLE_RATE
to enable profiling.
Set the desired sampling rate for profiling. Replace 0.3
with your desired sampling rate (0.3
means sample ~30% of your traffic):
define( 'WP_SENTRY_TRACES_SAMPLE_RATE', 0.3 ); // traces_sample_rate
// https://docs.sentry.io/platforms/php/profiling/#configure
define( 'WP_SENTRY_PROFILES_SAMPLE_RATE', 0.3 ); // profiles_sample_rate
Note: Do not set this constant or set the sample rate to 0.0
to disable the performance monitoring.
WP_SENTRY_BROWSER_FEEDBACK_OPTIONS
(Browser)You can enable the User Feedback Widget (official documentation) by adding this snippet to your wp-config.php
:
define( 'WP_SENTRY_BROWSER_FEEDBACK_OPTIONS', [ 'enabled' => true ] );
All option are passed directly to feedbackIntegration()
, you can read about all the available options in the official documentation.
This plugin provides the following filters to plugin/theme developers.
Note: Some filters are fired when the Sentry trackers are initialised so they won't fire if you define them in your theme or in a plugin that loads after WP Sentry does.
wp_sentry_user_context
(array)You can use this filter to extend the Sentry user context for both PHP and JS trackers.
WARNING: These values are exposed to the public in the JS tracker, so make sure you do not expose anything private!
Example usage:
add_filter( 'wp_sentry_user_context', function ( array $user ) {
return array_merge( $user, array(
'a-custom-user-meta-key' => 'custom value',
));
} );
Note: _This filter fires on the WordPress set_current_user
action and only if the WP_SENTRY_SEND_DEFAULT_PII
constant is set to true
._
wp_sentry_dsn
(string)You can use this filter to override the Sentry DSN used for the PHP tracker.
WARNING: This is not recommended, please set the DSN using the
WP_SENTRY_PHP_DSN
constant in yourwp-config.php
!
Example usage:
add_filter( 'wp_sentry_dsn', function ( $dsn ) {
return 'https://<key>:<secret>@sentry.io/<project>';
} );
Note: _This filter fires on the WordPress after_setup_theme
action. It is discouraged to use this and instead define the DSN in the wp-config.php
using the WP_SENTRY_PHP_DSN
constant_
wp_sentry_scope
(void)You can use this filter to customize the Sentry scope.
Example usage:
add_filter( 'wp_sentry_scope', function ( \Sentry\State\Scope $scope ) {
$scope->setTag('my-custom-tag', 'tag-value');
return $scope;
} );
Note: _This filter fires on the WordPress after_setup_theme
action._
wp_sentry_options
You can use this filter to customize the Sentry options.
Example usage:
add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
// Only sample 90% of the events
$options->setSampleRate(0.9);
return $options;
} );
Note: _This filter fires on the WordPress after_setup_theme
action._
wp_sentry_before_send
You can use this filter to filter error events sent to Sentry. Read more about filtering in the docs.
Example usage:
add_filter( 'wp_sentry_before_send', function ( \Sentry\Event $event, ?\Sentry\EventHint $hint = null ) {
// Don't send error event with level `warning` for the Hello Dolly example plugin
if ( $hint->exception !== null && $event->getLevel() === \Sentry\Severity::warning() && strpos( $hint->exception->getFile(), 'plugins/hello.php' ) !== false ) {
return null;
}
return $event;
}, 2 );
Note: Do not forget to return the $event
if you want to send it to Sentry, returning null
discards the event.
wp_sentry_public_dsn
(string)You can use this filter to override the Sentry DSN used for the JS tracker.
WARNING: This is not recommended, please set the DSN using the
WP_SENTRY_BROWSER_DSN
constant in yourwp-config.php
!
Example usage:
add_filter( 'wp_sentry_public_dsn', function ( $dsn ) {
return 'https://<key>@sentry.io/<project>';
} );
wp_sentry_public_options
(array)You can use this filter to customize/override the Sentry options used to initialize the JS tracker.
WARNING: These values are exposed to the public, so make sure you do not expose anything private !
Example usage:
add_filter( 'wp_sentry_public_options', function ( array $options ) {
return array_merge( $options, array(
'sampleRate' => '0.5',
'denyUrls' => array(
'https://github.com/',
'regex:\\w+\\.example\\.com',
),
));
} );
Note: Items prefixed with regex:
in denyUrls
, allowUrls
and ignoreErrors
option arrays will be translated into pure RegExp.
wp_sentry_public_context
(array)You can use this filter to customize/override the Sentry context, you can modify the user
and tags
context.
WARNING: These values are exposed to the public, so make sure you do not expose anything private !
Example usage:
add_filter( 'wp_sentry_public_context', function ( array $context ) {
$context['tags']['my-custom-tag'] = 'tag-value';
return $context;
} );
Many plugin in the WordPress ecosystem generate notices that are captured by the Sentry plugin.
This can cause a high volume of events and even slower page loads because of those events being transmitted to Sentry.
The prevent this you can set the following in your wp-config.php
to filter out errors of the notice type.
define( 'WP_SENTRY_ERROR_TYPES', E_ALL & ~E_NOTICE & ~E_USER_NOTICE );
Note: You can set any combination of error types you want, see the PHP documentation for more information.
The best thing to do with an exception is to capture and handle it, however you might still want to know an exception happened.
The Sentry plugin only captures unhandled exceptions and fatal errors, to capture handled exception you can do the following:
try {
myMethodThatCanThrowAnException();
} catch ( \Throwable $e ) {
// Option #1: Use the `captureException` or `captureMessage` action
// It's safe to call these actions even if the plugin is disabled (it will simply do nothing)
do_action( 'sentry/captureException', $e );
do_action( 'sentry/captureMessage', $e->getMessage() ); // it is recommended to use `captureException`
// Option #2: Use the `wp_sentry_safe` function to interact with the Sentry SDK directly
// It's advised to wrap this in a function_exists check to prevent errors when the plugin is disabled
if ( function_exists( 'wp_sentry_safe' ) ) {
wp_sentry_safe( function ( \Sentry\State\HubInterface $client ) use ( $e ) {
$client->captureException( $e );
} );
}
wp_die( 'There was an error doing this thing you were doing, we have been notified!' );
}
If you need to attach extra data only for the handled exception, you could add Structured Context:
$e = new Exception('Some exception I want to capture with extra data.');
if (function_exists('wp_sentry_safe')) {
wp_sentry_safe(function (\Sentry\State\HubInterface $client) use ($e) {
$client->withScope(function (\Sentry\State\Scope $scope) use ($client, $e) {
$scope->setContext('user_data', $e->getData());
$client->captureException($e);
});
});
}
If you need to add data to the scope in every case use configureScope
in wp_sentry_scope filter.
Since WP Sentry is a WordPress plugin it loads after WordPress and unless you are using a must-use plugin (see Capturing plugin errors) even after some other plugins loaded throwing errors which are not captured by Sentry.
To remedy this you can opt to load the plugin from your wp-config.php
file before WordPress is started.
It's really simple to do this by adding the following snippet to your wp-config.php
before the /* That's all, stop editing! Happy blogging. */
comment:
// It's possible your WordPress installation is different, check to make sure this path is correct for your installation
require_once __DIR__ . '/wp-content/plugins/wp-sentry-integration/wp-sentry.php';
Also make sure that any configuration options like WP_SENTRY_PHP_DSN
are set before the snippet above otherwise they have no effect.
Since this plugin is called wp-sentry-integration
it loads a bit late which could miss errors or notices occuring in plugins that load before it.
You can remedy this by loading WordPress Sentry as a must-use plugin by creating the file wp-content/mu-plugins/wp-sentry-integration.php
(if the mu-plugins
directory does not exist you must create that too).
<?php
/**
* Plugin Name: WordPress Sentry
* Plugin URI: https://github.com/stayallive/wp-sentry
* Description: A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.
* Version: must-use-proxy
* Author: Alex Bouma
* Author URI: https://alex.bouma.dev
* License: MIT
*/
$wp_sentry = WP_CONTENT_DIR . '/plugins/wp-sentry-integration/wp-sentry.php';
// Do not crash in case the plugin is not installed
if ( ! file_exists( $wp_sentry ) ) {
return;
}
require $wp_sentry;
Now wp-sentry-integration
will load always and before all other plugins.
Note: It is advised you leave the original wp-sentry-integration
in the /wp-content/plugins
folder to still have updates come in through the WordPress updater. However enabling or disabling does nothing if the above script is active (since it will always be enabled).
This is an example on how to use the before_send
callback of the Sentry SDK to only capture errors occuring in a certain theme or plugin.
See also the filter docs: wp_sentry_option.
add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
$options->setBeforeSendCallback( function ( \Sentry\Event $event ) {
$exceptions = $event->getExceptions();
// No exceptions in the event? Send the event to Sentry, it's most likely a log message
if ( empty( $exceptions ) ) {
return $event;
}
$stacktrace = $exceptions[0]->getStacktrace();
// No stacktrace in the first exception? Send it to Sentry just to be safe then
if ( $stacktrace === null ) {
return $event;
}
// Little helper and fallback for PHP versions without the str_contains function
$strContainsHelper = function ( $haystack, $needle ) {
if ( function_exists( 'str_contains' ) ) {
return str_contains( $haystack, $needle );
}
return $needle !== '' && mb_strpos( $haystack, $needle ) !== false;
};
foreach ( $stacktrace->getFrames() as $frame ) {
// Check the the frame happened inside our theme or plugin
// Change THEME_NAME and PLUGIN_NAME to whatever is required
// And / or modify this `if` statement to detect other variables
if ( $strContainsHelper( $frame->getFile(), 'themes/THEME_NAME' )
|| $strContainsHelper( $frame->getFile(), 'plugins/PLUGIN_NAME' )
) {
// Send the event to Sentry
return $event;
}
}
// Stacktrace contained no frames in our theme and/or plugin? We send nothing to Sentry
return null;
} );
return $options;
} );
When using the Sentry Browser integration it is possible to do some work in the client browser before Sentry is initialized to change options and/or prevent the Browser SDK from initializing at all.
You do this by defining a wp_sentry_hook
JavaScript function before the Sentry Browser JavaScript file is included (keep this function small and easy since any errors that occur in there are not tracked by the Browser SDK).
A quick example on how you would disable the Browser SDK using wp_add_inline_script
:
add_action( 'wp_enqueue_scripts', function () {
wp_add_inline_script( 'wp-sentry-browser', 'function wp_sentry_hook(options) { return someCheckInYourCode() ? true : false; }', 'before' );
} );
When the wp_sentry_hook
function returns false
the initialization of the Sentry Brower SDK will be stopped. Any other return value will be ignored.
To modify the options you can modify the object passed as the first argument of the wp_sentry_hook
, this object will later be passed to Sentry.init
to initialize the Browser SDK.
ClientBuilder
or options before initialisationBecause the PHP SDK is initialized as quick as possible to capture early errors, it's impossible to modify the options or the ClientBuilder
before the initialization with WordPress hooks.
There exists a way to modify the options and the ClientBuilder
before the initialization of the PHP SDK by setting a callback using a constant called WP_SENTRY_CLIENTBUILDER_CALLBACK
.
The callback will be executed whenever the plugin creates a new ClientBuilder
instance to create a new PHP SDK client.
You would place the example below in your wp-config.php
file to make sure it's available before the PHP SDK is initialized:
function wp_sentry_clientbuilder_callback( \Sentry\ClientBuilder $builder ): void {
// For example, disabling the default integrations
$builder->getOptions()->setDefaultIntegrations( false );
}
define( 'WP_SENTRY_CLIENTBUILDER_CALLBACK', 'wp_sentry_clientbuilder_callback' );
If you need to use a HTTP proxy to send events to Sentry you can setup the WordPress HTTP proxy support and this plugin will transfer those settings to the Sentry SDK.
Read more about how to setup the WordPress Proxy constants here: https://developer.wordpress.org/reference/classes/wp_http_proxy/#description.
There is one caveat, if you are using an HTTP proxy in WordPress but don't want Sentry to use it you should set WP_SENTRY_PROXY_ENABLED
to false
in your wp-config.php
. Setting WP_PROXY_BYPASS_HOSTS
will not work!
If you discover a security vulnerability within WordPress Sentry (wp-sentry), please send an e-mail to Alex Bouma at alex+security@bouma.me
. All security vulnerabilities will be swiftly addressed.
The WordPress Sentry (wp-sentry) plugin is open-sourced software licensed under the MIT license.