jan_dembowski

add-prettyphoto.php

Oct 20th, 2013
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: PrettyPhoto for galleries
  4. Description: This plugin adds rel=&quot;prettyPhoto&quot; for galleries
  5. Author: Jan Dembowski
  6. */
  7.  
  8. add_filter( 'the_content', 'mh_add_prettyphoto' , 15 );
  9.  
  10. function mh_add_prettyphoto( $text ) {
  11.  
  12.         // RegEx to find the right <a href=...><img data-attachment-id=... and put that into an array
  13.         $mh_regex = "/<a href=\"(http|https):\/\/[a-zA-Z0-9-.\/\?=]+\"><img\ data-attachment-id=/";
  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
  19.         for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
  20.                 {
  21.                         $mh_old = $mh_matches[0][$mh_count];
  22.                         $mh_new = str_replace( '><img data' , ' rel="prettyPhoto"><img data' , $mh_old );
  23.                         $text = str_replace( $mh_old  , $mh_new , $text );
  24.                 }
  25.         // Return any substitutions
  26.         return $text;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment