PHPackages                             stayallive/wp-sentry - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. stayallive/wp-sentry

ActiveWordpress-plugin[Logging &amp; Monitoring](/categories/logging)

stayallive/wp-sentry
====================

A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.

v8.10.0(6mo ago)379197.9k—3.8%53[3 PRs](https://github.com/stayallive/wp-sentry/pulls)MITPHPPHP ^7.2.5 || ^8.0CI passing

Since Oct 29Pushed 2mo ago10 watchersCompare

[ Source](https://github.com/stayallive/wp-sentry)[ Packagist](https://packagist.org/packages/stayallive/wp-sentry)[ Docs](https://github.com/stayallive/wp-sentry)[ GitHub Sponsors](https://github.com/stayallive)[ RSS](/packages/stayallive-wp-sentry/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (147)Used By (0)

[Sentry for WordPress](https://wordpress.org/plugins/wp-sentry-integration/) (wp-sentry)
========================================================================================

[](#sentry-for-wordpress-wp-sentry)

A (unofficial) [WordPress plugin](https://wordpress.org/plugins/wp-sentry-integration/) to report PHP and JavaScript errors to [Sentry](https://sentry.io).

*This plugin was called "WordPress Sentry" before 2025-09-17 but was renamed to "Sentry for WordPress" to comply with trademark claims from The WordPress Foundation. Same plugin, slightly different name.*

What?
-----

[](#what)

This plugin can report PHP errors (optionally) and JavaScript errors (optionally) to [Sentry](https://sentry.io) 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.

Requirements
------------

[](#requirements)

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: .

- Version `2.1.*` of this plugin will be the last to support PHP `5.3`.
- Version `2.2.*` of this plugin will be the last to support PHP `5.4`.
- Version `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.

- Version [`2.x`](https://github.com/stayallive/wp-sentry/tree/2.x) of the wp-sentry plugin uses the [`1.x`](https://github.com/getsentry/sentry-php/tree/1.x) version of the official Sentry PHP SDK.
- Version [`3.x`](https://github.com/stayallive/wp-sentry/tree/3.x) of the wp-sentry plugin uses the [`2.x`](https://github.com/getsentry/sentry-php/tree/2.x) version of the official Sentry PHP SDK.
- Version [`4.x`](https://github.com/stayallive/wp-sentry/tree/4.x), [`5.x`](https://github.com/stayallive/wp-sentry/tree/5.x), &amp; [`6.x`](https://github.com/stayallive/wp-sentry/tree/6.x) of the wp-sentry plugin uses the [`3.x`](https://github.com/getsentry/sentry-php/tree/3.x) version of the official Sentry PHP SDK.
- Version [`7.x`](https://github.com/stayallive/wp-sentry/tree/master)+ of the wp-sentry plugin uses the [`4.x`](https://github.com/getsentry/sentry-php/tree/master) version of the official Sentry PHP SDK.

Usage
-----

[](#usage)

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.

### WordPress plugin repository

[](#wordpress-plugin-repository)

1. Install this plugin from the WordPress plugin repository:
2. Configure your DSN as explained in the [configuration](#configuration) section
3. Activate the plugin through the WordPress admin interface or wp-cli

### Manual plugin installation

[](#manual-plugin-installation)

1. Download the plugin from the [releases page](https://github.com/stayallive/wp-sentry/releases)
2. Extract it and place the folder in your `wp-content/plugins` folder
3. Configure your DSN as explained in the [configuration](#configuration) section
4. Activate the plugin through the WordPress admin interface or wp-cli

### Using composer

[](#using-composer)

1. Run `composer require stayallive/wp-sentry` in your project
2. Configure your DSN as explained in the [configuration](#configuration) section
3. Activate the plugin through the WordPress admin interface or wp-cli

Configuration
-------------

[](#configuration)

To start using the plugin first setup the [DSN](#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!

### DSN

[](#dsn)

Sentry uses something called a DSN ([read more](https://docs.sentry.io/product/sentry-basics/dsn-explainer/)) to configure the SDK.

#### `WP_SENTRY_PHP_DSN` (PHP)

[](#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)

[](#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.

### Privacy

[](#privacy)

#### `WP_SENTRY_SEND_DEFAULT_PII`

[](#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 );
```

### Options

[](#options)

#### `WP_SENTRY_VERSION`

[](#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.10.0' );
```

#### `WP_SENTRY_ENV`

[](#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)

[](#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](https://www.php.net/manual/en/errorfunc.constants.php) for more information.

### Tracing

[](#tracing)

#### `WP_SENTRY_TRACES_SAMPLE_RATE` (PHP)

[](#wp_sentry_traces_sample_rate-php)

Set the desired sampling rate for performance 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)

[](#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)

[](#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)

[](#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.

### Profiling

[](#profiling)

#### `WP_SENTRY_PROFILES_SAMPLE_RATE` (PHP)

[](#wp_sentry_profiles_sample_rate-php)

**Note:** This feature requires the Excimer PHP extension to be installed. See the [Sentry PHP SDK documentation](https://docs.sentry.io/platforms/php/profiling/#configure) 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.

### User Feedback

[](#user-feedback)

#### `WP_SENTRY_BROWSER_FEEDBACK_OPTIONS` (Browser)

[](#wp_sentry_browser_feedback_options-browser)

You can enable the User Feedback Widget ([official documentation](https://docs.sentry.io/platforms/javascript/user-feedback/#user-feedback-widget)) 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](https://docs.sentry.io/platforms/javascript/user-feedback/configuration/#user-feedback-widget).

### Logs

[](#logs)

#### `WP_SENTRY_ENABLE_LOGS` (PHP)

[](#wp_sentry_enable_logs-php)

You can enable sending Logs ([official documentation](https://docs.sentry.io/platforms/php/logs/)) by adding this snippet to your `wp-config.php`:

```
define( 'WP_SENTRY_ENABLE_LOGS', true );
```

When enabled you can use the `\Sentry\logger()` function to log messages to Sentry:

Filters
-------

[](#filters)

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.

### Common to PHP &amp; Browser

[](#common-to-php--browser)

#### `wp_sentry_user_context` (array)

[](#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`.*

### Specific to PHP

[](#specific-to-php)

#### `wp_sentry_dsn` (string)

[](#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 your `wp-config.php`!

Example usage:

```
add_filter( 'wp_sentry_dsn', function ( $dsn ) {
	return 'https://:@sentry.io/';
} );
```

**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)

[](#wp_sentry_scope-void)

You can use this filter to customize the Sentry [scope](https://docs.sentry.io/platforms/php/enriching-events/context/).

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`

[](#wp_sentry_options)

You can use this filter to customize the Sentry [options](https://docs.sentry.io/platforms/php/configuration/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`

[](#wp_sentry_before_send)

You can use this filter to filter error events sent to Sentry. Read more about [filtering in the docs](https://docs.sentry.io/platforms/php/configuration/filtering/#filtering-error-events).

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.*

### Specific to Browser

[](#specific-to-browser)

#### `wp_sentry_public_dsn` (string)

[](#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 your `wp-config.php`!

Example usage:

```
add_filter( 'wp_sentry_public_dsn', function ( $dsn ) {
	return 'https://@sentry.io/';
} );
```

#### `wp_sentry_public_options` (array)

[](#wp_sentry_public_options-array)

You can use this filter to customize/override the Sentry [options](https://docs.sentry.io/platforms/javascript/configuration/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)

[](#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;
} );
```

Advanced usages
---------------

[](#advanced-usages)

### High volume of notices

[](#high-volume-of-notices)

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](https://www.php.net/manual/en/errorfunc.constants.php) for more information.

### Capturing handled exceptions

[](#capturing-handled-exceptions)

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](https://docs.sentry.io/platforms/php/enriching-events/context/#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](#wp_sentry_scope-void).

### Loading Sentry before WordPress

[](#loading-sentry-before-wordpress)

Since WP Sentry is a WordPress plugin it loads after WordPress and unless you are using a must-use plugin (see [Capturing plugin errors](#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.

### Capturing plugin errors

[](#capturing-plugin-errors)

Since this plugin is called `wp-sentry-integration` it loads a bit late which could miss errors or notices occurring in plugins that load before it.

You can remedy this by loading Sentry for WordPress 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).

```
