1. Go to this page and download the library: Download stayallive/wp-sentry library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stayallive / wp-sentry example snippets
define( 'WP_SENTRY_PHP_DSN', 'PHP_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
add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
// Only sample 90% of the events
$options->setSampleRate(0.9);
return $options;
} );
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 );
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!' );
}
$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);
});
});
}
// It's possible your WordPress installation is different, check to make sure this path is correct for your installation
/**
* 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;
}
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