PHP code example of error-explorer / wordpress-error-reporter

1. Go to this page and download the library: Download error-explorer/wordpress-error-reporter 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/ */

    

error-explorer / wordpress-error-reporter example snippets




use ErrorExplorer\WordPressErrorReporter\ErrorReporter;

$errorReporter = new ErrorReporter('YOUR_WEBHOOK_URL', [
    'environment' => wp_get_environment_type(),
    'project_name' => 'My WordPress Site',
    'capture_request_data' => true,
    'capture_session_data' => true,
    'capture_server_data' => true,
]);

$errorReporter->register();

$config = [
    'environment' => wp_get_environment_type(), // or 'production', 'staging', etc.
    'project_name' => 'My WordPress Site',
    'capture_request_data' => true,
    'capture_session_data' => true,
    'capture_server_data' => true,
    'max_breadcrumbs' => 20,
];

$errorReporter = new ErrorReporter('YOUR_WEBHOOK_URL', $config);
$errorReporter->register();

// Report an exception
try {
    // Your code here
    throw new Exception('Something went wrong');
} catch (Exception $e) {
    $errorReporter->reportError($e, wp_get_environment_type(), 500);
}

// Report a custom message
$errorReporter->reportMessage(
    'Custom error message',
    wp_get_environment_type(),
    null, // HTTP status (optional)
    'error', // Level: error, warning, info
    ['user_id' => get_current_user_id()] // Additional context
);

// Add a custom breadcrumb
$errorReporter->addBreadcrumb('User logged in', 'auth', 'info', [
    'user_id' => get_current_user_id()
]);

// Log navigation
$errorReporter->logNavigation('/wp-admin/', '/wp-admin/edit.php');

// Log user actions
$errorReporter->logUserAction('post_published', [
    'post_id' => $post->ID,
    'post_type' => $post->post_type
]);

// Log HTTP requests
$errorReporter->logHttpRequest('POST', '/wp-admin/admin-ajax.php', 200, [
    'action' => $_POST['action']
]);

add_action('after_setup_theme', function() {
    if (class_exists('ErrorExplorer\WordPressErrorReporter\ErrorReporter')) {
        $webhook_url = get_option('error_explorer_webhook_url');
        
        if ($webhook_url && get_option('error_explorer_enabled')) {
            $errorReporter = new \ErrorExplorer\WordPressErrorReporter\ErrorReporter($webhook_url, [
                'environment' => wp_get_environment_type(),
                'project_name' => get_bloginfo('name'),
            ]);
            
            $errorReporter->register();
        }
    }
});

class MyPlugin {
    private $errorReporter;
    
    public function __construct() {
        add_action('plugins_loaded', [$this, 'init_error_reporting']);
    }
    
    public function init_error_reporting() {
        if (class_exists('ErrorExplorer\WordPressErrorReporter\ErrorReporter')) {
            $this->errorReporter = new \ErrorExplorer\WordPressErrorReporter\ErrorReporter(
                'YOUR_WEBHOOK_URL',
                ['project_name' => 'My Plugin']
            );
            
            $this->errorReporter->register();
        }
    }
    
    public function some_method() {
        try {
            // Plugin logic here
        } catch (Exception $e) {
            if ($this->errorReporter) {
                $this->errorReporter->addBreadcrumb('Plugin method failed', 'plugin');
                $this->errorReporter->reportError($e);
            }
            throw $e; // Re-throw if needed
        }
    }
}

// Track order failures
add_action('woocommerce_order_status_failed', function($order_id) {
    global $errorReporter;
    if ($errorReporter) {
        $errorReporter->addBreadcrumb('Order failed', 'woocommerce', 'error', [
            'order_id' => $order_id
        ]);
        
        $errorReporter->reportMessage(
            'WooCommerce order failed',
            wp_get_environment_type(),
            null,
            'warning',
            ['order_id' => $order_id]
        );
    }
});

// Track payment errors
add_action('woocommerce_payment_failure', function() {
    global $errorReporter;
    if ($errorReporter) {
        $errorReporter->addBreadcrumb('Payment failed', 'woocommerce', 'error');
    }
});

// Test exception
try {
    throw new Exception('Test error from Error Explorer WordPress SDK');
} catch (Exception $e) {
    $errorReporter->reportError($e);
}

// Test PHP error
$undefined_variable->someProperty; // Will be caught by error handler

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
bash
cd test-wordpress-project
php test-errors.php