Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Add target="webapp" to links
- Description: This plugin will add target="webapp" to links in the post and page content as well as in the comments.
- Author: Jan Dembowski
- Author URI: http://blog.dembowski.net/
- */
- add_filter( 'the_content' , 'mh_webapp' , 50 );
- add_filter( 'comment_text' , 'mh_webapp' , 50 );
- add_action( 'wp_head' , 'mh_base' );
- function mh_webapp( $content ) {
- //
- // Idea harvested from here http://css-tricks.com/snippets/php/find-urls-in-text-make-links/
- //
- $mh_url_regex = "/\<a\ href\=\"(http|https)\:\/\/[a-zA-Z0-9\-\.]+[a-zA-Z]{2,3}/";
- preg_match_all( $mh_url_regex , $content, $mh_matches );
- for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
- {
- $mh_old_url = $mh_matches[0][$mh_count];
- $mh_new_url = str_replace( ' href="' , ' target="webapp" href="' , $mh_matches[0][$mh_count] );
- // Ignore any links that already have target="webapp"
- if( !preg_match( '/target\=\"webapp\"/' , $mh_old_url , $mh_dont_care ) )
- $content = str_replace( $mh_old_url , $mh_new_url , $content );
- }
- return $content;
- }
- function mh_base() {
- echo '<base target="webapp" />';
- }
Advertisement
Add Comment
Please, Sign In to add comment