Skip to content

Instantly share code, notes, and snippets.

@MaximeMichaud
Created January 24, 2026 23:56
Show Gist options
  • Select an option

  • Save MaximeMichaud/402b65ddf5fa3b95ae63388c7a36dc72 to your computer and use it in GitHub Desktop.

Select an option

Save MaximeMichaud/402b65ddf5fa3b95ae63388c7a36dc72 to your computer and use it in GitHub Desktop.
WP Mail SMTP Link Redirector : Handles old WP Mail SMTP tracking links after plugin removal
<?php
/**
* Plugin Name: WP Mail SMTP Link Redirector
* Description: Handles redirections for old WP Mail SMTP tracking links
* Version: 1.0.0
* Author: Maxime Michaud
* Author URI: https://github.com/MaximeMichaud
*/
// Security: prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Register REST API endpoint to handle old links
add_action( 'rest_api_init', function() {
register_rest_route(
'wp-mail-smtp/v1',
'/e/(?P<data>.+)',
[
'methods' => 'GET',
'callback' => 'wpms_handle_tracking_redirect',
'permission_callback' => '__return_true',
]
);
});
/**
* Handle tracking link redirection
*/
function wpms_handle_tracking_redirect( $request ) {
// Decode the data
$encoded_data = $request->get_param( 'data' );
$data = base64_decode( $encoded_data );
parse_str( $data, $args );
// Debug: view data structure (comment out after testing)
// error_log( 'WPMS Redirect Debug: ' . print_r( $args, true ) );
// Check if URL exists in the data
if ( isset( $args['data']['url'] ) ) {
$url = urldecode( $args['data']['url'] );
// Sanitize the URL
$url = esc_url_raw( $url );
// Create redirect response
$response = new WP_REST_Response();
$response->set_status( 301 );
$response->header( 'Location', $url );
$response->header( 'Cache-Control', 'no-cache, no-store, must-revalidate' );
$response->header( 'Pragma', 'no-cache' );
return $response;
}
// If no URL found, redirect to homepage
$response = new WP_REST_Response();
$response->set_status( 302 );
$response->header( 'Location', home_url() );
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment