Advertisement
Guest User

Convert iTunes links to TradeDoubler affiliate links

a guest
Jun 4th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. /**
  2.  * Convert iTunes links to TradeDoubler affiliate links
  3.  */
  4. add_filter( 'the_content', 'themename_music_link_filter', 1 );
  5. add_filter( 'comment_text', 'themename_music_link_filter' );
  6.  
  7. function themename_music_link_filter( $content ) {
  8.     $content = preg_replace_callback( '/<a\b(?>\s+(?:href="([^"]*)")|[^\s>]+|\s+)*>/', 'themename_affiliatize_links', $content );
  9.    
  10.     return $content;
  11. }
  12.  
  13. function themename_affiliatize_links( $matches ) {
  14.     $link_tag = $matches[0];
  15.    
  16.     if  ( isset( $matches[1] ) ) {
  17.         if ( 0 === strpos( $matches[1], 'http://itunes.apple.com/' ) || 0 === strpos( $matches[1], 'http://phobos.apple.com/' ) ) {
  18.             $wrapper = 'http://clkuk.tradedoubler.com/click?p=23708&a=1995293&url=';
  19.             $new_url = add_query_arg( 'partnerId', 2003, $matches[1 ]);
  20.             $new_url = $wrapper . rawurlencode( rawurlencode( $new_url ) );
  21.             $link_tag = str_replace( $matches[1], $new_url, $matches[0] );
  22.         }
  23.     }
  24.    
  25.     return $link_tag;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement