title: URL Cloaking in WordPress Plugin
body: <p>I'm planning to obscure link urls with the following way. It stores urls in an array and then if it detects the parameter in the $_GET array, it redirects to the saved url. </p>
  
  <pre><code>&lt;?
  /* Plugin Name: Sample Link Cloak  */
  
  add_action('admin_menu', 'sample_link_cloak');
  function sample_link_cloak() {
      add_options_page(
          'Sample Link Cloak', 
          'Sample Link Cloak', 
          'manage_options',
          'sample_link_cloak', 
          'sample_link_cloak_admin');
  }
  function sample_link_cloak_admin() {
      ?&gt;
      &lt;div class="wrap"&gt;
      &lt;?php
          $links = '&lt;p&gt;&lt;a href="http://www.google.com"&gt;google.com&lt;/a&gt;&lt;/p&gt;' . PHP_EOL
                  . '&lt;p&gt;&lt;a href="http://www.wordpress.org"&gt;wordpress.org&lt;/a&gt;&lt;/p&gt;' . PHP_EOL
                  . '&lt;p&gt;&lt;a href="http://www.stackoverflow.com"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;' . PHP_EOL;
  
          $doc = new DOMDocument();   
          @$doc-&gt;loadHTML($links);    
          $array_urls = array();
          foreach ($doc-&gt;getElementsByTagName('a') as $node) {
              $url = $node-&gt;getAttribute('href');
              $hash = md5($url);
              $array_urls[$hash] = $url;
              $url = site_url('?urlcloak=' . $hash);
              $node-&gt;setAttribute('href', $url);
          }       
          echo $doc-&gt;saveXML();
          update_option('sample_urlcloak', $array_urls);
      ?&gt;
      &lt;/div&gt;
      &lt;?php
  }
  add_action('init', 'cloakurls');
  function cloakurls() {
      if (isset($_GET['urlcloak'])) {
          $array_urls = get_option('sample_urlcloak');
          wp_redirect($array_urls[$_GET['urlcloak']]);
          exit;       
      }
  }
  </code></pre>
  
  <p>There is a problem I can predict with this method. The number of links increases day by day so the stored data have to be deleted when they reach to some extent. But if the visitor saves the web page on the local drive and read the contents later on and click the link, at this point if the server does not store the url, it won't redirect. </p>
  
  <p>I think it is pretty much the same issue described here. <a href="https://stackoverflow.com/questions/4307155/looking-for-more-efficient-way-to-serve-numerous-link-redirects">Looking for more efficient way to serve numerous link redirects?</a> but for a distributed plugin, is it realistic/practical to edit the .htaccess file? I guess not all users have the right access to modify .htaccess. I'm not sure.</p>
  
  <p>I'm wondering if somebody can direct me to the right direction.</p>