If you publish technical guides, reviews, or product-driven articles, you probably paste Amazon URLs many times a week. As the number of posts grows, keeping these URLs consistent—especially with tracking parameters—becomes difficult.
To solve this, I added a WordPress-side mechanism that automatically optimizes Amazon product URLs only when they are newly inserted, without altering anything already published.
This simple automation drastically reduced link management overhead.

Why automated optimization helps when using Amazon URLs
Manual URL editing seems easy at first, but creates several long-term issues:
- Tracking parameters get forgotten
- Formatting varies between writers
- Old posts retain outdated link styles
- Fixing links requires reopening past articles
An automated rule applied at the WordPress level removes all of these issues.
The system works by following these principles:
- Existing Amazon URLs remain untouched
- Only newly added URLs are optimized
- If a URL already contains a tracking parameter, it is preserved
- Only Amazon product pages (/dp/xxxx) are targeted
- Writers only need to paste the URL—nothing more
This creates a stable, consistent workflow regardless of website scale.
The actual WordPress code that handles Amazon link optimization
Add this to your child theme’s functions.php:
/**
* Automatically optimize only newly added Amazon product links.
* Existing URLs remain intact.
*/
function auto_optimize_amazon_links($content, $post_id) {
// Tracking parameter (example)
$auto_param = 'tag=yourtag-22';
// Old content (to detect pre-existing URLs)
$old = get_post_field('post_content', $post_id, 'raw');
// Regex to capture Amazon URLs
$pattern = '/https?:\/\/(?:www\.)?amazon\.co\.jp\/[^\s"\']+/u';
return preg_replace_callback($pattern, function ($matches) use ($old, $auto_param) {
$url = $matches[0];
// If this URL existed before, respect it
if (strpos($old, $url) !== false) {
return $url;
}
// Do not overwrite URLs that already include "tag="
if (strpos($url, 'tag=') !== false) {
return $url;
}
// Process only product pages
if (!preg_match('/\/dp\/[A-Za-z0-9]+/i', $url)) {
return $url;
}
// Add parameter safely
$delimiter = (strpos($url, '?') !== false) ? '&' : '?';
return $url . $delimiter . $auto_param;
}, $content);
}
add_filter('content_save_pre', 'auto_optimize_amazon_links', 10, 2);
What changes after implementing this system
After introducing this logic, link management essentially disappeared from my workflow.
- Writers only need to paste Amazon URLs
- Tracking consistency becomes automatic
- No risk of breaking existing posts
- Link quality remains stable even as the site grows
For websites frequently referencing Amazon products, this automation becomes a structural advantage.
参考URL
- WordPress Codex – Filters
https://developer.wordpress.org/plugins/hooks/filters/ - PHP preg_replace_callback
https://www.php.net/manual/en/function.preg-replace-callback.php - WordPress content_save_pre
https://developer.wordpress.org/reference/hooks/content_save_pre/