title: Wordpress: Automatically change specific URLs in posts
body: <p>I have found a solution to change links in my wordpress theme, but not the links in the content. How is it possible to get the URL in the content, so I can also changed them?</p>
  
  <p>I need to use <code>the content</code> filter. But how is it possible to change URLs like apple.com/test/ apple.com/test-123/, apple.com, microsoft.com, microsoft.com/test/. The function should also change correctly every matched URL in the content.</p>
  
  <pre><code>add_filter('the_content ', 'function_name');
  </code></pre>
  
  <p>The <a href="https://wordpress.stackexchange.com/questions/357790/change-links-automatically-to-affiliate-links">answer of a similiar question</a> unfortunately doesn't work.</p>
  
  <p>This is my working solution to change links, but not the links in the content.</p>
  
  <pre class="lang-php prettyprint-override"><code>add_filter('rh_post_offer_url_filter', 'link_change_custom');
  function link_change_custom($offer_post_url){
  
  $shops= array(
          array('shop'=&gt;'apple.com','id'=&gt;'1234'),
          array('shop'=&gt;'microsoft.com','id'=&gt;'5678'),
          array('shop'=&gt;'dell.com','id'=&gt;'9876'), 
      );
      foreach( $shops as $rule ) {
          if (!empty($offer_post_url) &amp;&amp; strpos($offer_post_url, $rule['shop']) !== false) {      
              $offer_post_url = 'https://www.network.com/promotion/click/id='.$rule['id'].'-yxz?param0='.rawurlencode($offer_post_url);
  }    
      }
  $shops2= array(
          array('shop'=&gt;'example.com','id'=&gt;'1234'),
          array('shop'=&gt;'domain2.com','id'=&gt;'5678'),
          array('shop'=&gt;'domain3','id'=&gt;'9876'),  
      );
      foreach( $shops2 as $rule ) {
          if (!empty($offer_post_url) &amp;&amp; strpos($offer_post_url, $rule['shop']) !== false) {      
              $offer_post_url = 'https://www.second-network.com/promotion/click/id='.$rule['id'].'-yxz?param0='.rawurlencode($offer_post_url);
  }    
      }
  
          return $offer_post_url; 
  }
  </code></pre>