コンテンツにスキップ
Zidooka
スポンサーリンク

Making Amazon Links Smarter in WordPress: A System That Optimizes Only Newly Added URLs

* If you need help with the content of this article for work or development, individual support is available.

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

  1. WordPress Codex – Filters
    https://developer.wordpress.org/plugins/hooks/filters/
  2. PHP preg_replace_callback
    https://www.php.net/manual/en/function.preg-replace-callback.php
  3. WordPress content_save_pre
    https://developer.wordpress.org/reference/hooks/content_save_pre/
スポンサーリンク

ZIDOOKA!

Need help with the content of this article?

I provide individual technical support related to the issues described in this article, as a freelance developer.
If the problem is blocking your work or internal tasks, feel free to reach out.

Support starts from $30 USD (Estimate provided in advance)

Policy on AI Usage

Some articles on this site are written with the assistance of AI. However, we do not rely entirely on AI for writing; it is used strictly as a support tool. We believe that if using AI improves productivity and helps convey the message more effectively, it should be utilized.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です