jan_dembowski

add-webapp-to-links.php

Mar 22nd, 2012
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Add target="webapp" to links
  4. Description: This plugin will add target="webapp" to links in the post and page content as well as in the comments.
  5. Author: Jan Dembowski
  6. Author URI: http://blog.dembowski.net/
  7. */
  8. add_filter( 'the_content' , 'mh_webapp' , 50 );
  9. add_filter( 'comment_text' , 'mh_webapp' , 50 );
  10. add_action( 'wp_head' , 'mh_base' );
  11.  
  12. function mh_webapp( $content ) {
  13. //
  14. // Idea harvested from here http://css-tricks.com/snippets/php/find-urls-in-text-make-links/
  15. //
  16. $mh_url_regex = "/\<a\ href\=\"(http|https)\:\/\/[a-zA-Z0-9\-\.]+[a-zA-Z]{2,3}/";
  17.  
  18. preg_match_all( $mh_url_regex , $content, $mh_matches );
  19.  
  20. for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
  21.     {
  22.     $mh_old_url = $mh_matches[0][$mh_count];
  23.     $mh_new_url = str_replace( ' href="' , ' target="webapp" href="' , $mh_matches[0][$mh_count] );
  24.  
  25.     // Ignore any links that already have target="webapp"
  26.     if( !preg_match( '/target\=\"webapp\"/' , $mh_old_url , $mh_dont_care ) )
  27.         $content = str_replace( $mh_old_url , $mh_new_url , $content );
  28.     }
  29.  
  30. return $content;
  31. }
  32.  
  33. function mh_base() {
  34.         echo '<base target="webapp" />';
  35. }
Advertisement
Add Comment
Please, Sign In to add comment