jan_dembowski

tag-to-link.php

Aug 31st, 2013
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Convert Comment tag to link
  4. Description: Convert #tag to links in the comment_text area using a filter
  5. Author: Jan Dembowski
  6. */
  7.  
  8. add_filter( 'comment_text', 'mh_commenttaglink' , 50 );
  9.  
  10. function mh_commenttaglink( $text ) {
  11.  
  12.         // RegEx to find #tag, #hyphen-tag with letters and numbers
  13.         $mh_regex = "/\ #[a-zA-Z0-9-]+/";
  14.  
  15.         // Use that RegEx and populate the hits into an array
  16.         preg_match_all( $mh_regex , $text , $mh_matches );
  17.  
  18.         // If there's any hits then loop though those and replace those hits with a link
  19.         for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
  20.                 {
  21.                         $mh_old = $mh_matches[0][$mh_count];
  22.                         $mh_old_lesshash = str_replace( ' #' , '' , $mh_old );
  23.                         $mh_new = str_replace( $mh_old , '<a href="' . get_bloginfo( url ) . '/tag/' . $mh_old_lesshash . '"/ rel="tag">' . $mh_old . '</a>' , $mh_matches[0][$mh_count] );
  24.                         $text = str_replace( $mh_old  , $mh_new , $text );
  25.                 }
  26.         // Return any substitutions
  27.         return $text;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment