Advertisement
jan_dembowski

wrap-img-pinterest.php

Dec 2nd, 2012
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Pinterest Img Tag Button
  4. Description: This will wrap images in the_content with Pinterest Button code
  5. Author: Jan Dembowski
  6. Author URI: http://blog.dembowski.net/
  7. Version: 0.5
  8.  
  9. From http://wordpress.org/support/topic/add-code-before-each-image
  10.  
  11. */
  12.  
  13. add_filter( 'the_content' , 'mh_wrap_image' , 15 );
  14.  
  15. function mh_wrap_image( $content ) {
  16.  
  17. // Regex to find all <img ... > tags
  18. $mh_img_regex1 = "/\<img [^>]*src=\"([^\"]+)\"[^>]*>/";
  19.  
  20. // Regex to find all <a href"..."><img ... ></a> tags
  21. $mh_img_regex2 = "/<a href=.*><img [^>]*src=\"([^\"]+)\"[^>]*><\/a>/";
  22.  
  23. // Populate the results into 2 arrays
  24. preg_match_all( $mh_img_regex1 , $content, $mh_img );
  25. preg_match_all( $mh_img_regex2 , $content, $mh_matches );
  26.  
  27. // The second array will be a subset of the first so go through
  28. // each element and delete the duplicates in the first.
  29. $i=0;
  30. foreach ( $mh_img[0] as $mh_img_count ) {
  31.     $i2=0;
  32.     foreach ( $mh_matches[0] as $mh_matches_count ) {
  33.         if ( strpos($mh_matches_count, $mh_img_count ) ){
  34.             unset( $mh_img[0][$i] );
  35.             unset( $mh_img[1][$i] );
  36.                 $i2++;
  37.             break;
  38.         }
  39.             $i2++;
  40.             }
  41.     $i++;
  42.     }
  43. // There is almost certainly a better way to do this.
  44. // Append the no links array to the $mh_matches array.
  45. $i=0;
  46. $mh_start = count( $mh_matches[0] );
  47. foreach ( $mh_img[0] as $mh_img_count ) {
  48.     $mh_matches[0][ $mh_start + $i ] = $mh_img_count;
  49.     $i++;
  50. }
  51. $i=0;
  52. foreach ( $mh_img[1] as $mh_img_count ) {
  53.     $mh_matches[1][ $mh_start + $i ] = $mh_img_count;
  54.     $i++;
  55. }
  56.  
  57. // If we get any hits then put the code before and after the img tags
  58. if ( $mh_matches ) {;
  59.     for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
  60.         {
  61.         // Old img tag
  62.         $mh_old = $mh_matches[0][$mh_count];
  63.  
  64.         // Get the img URL, it's needed for the button code
  65.         $mh_img_url = $mh_matches[1][$mh_count];
  66.  
  67.         // Put together the pinterest code to place before the img tag
  68.         $mh_pinterest_code = '<span class="pinterest-button"><a href="http://pinterest.com/';
  69.         $mh_pinterest_code .= 'pin/create/button/?url=' . urlencode( get_permalink() );
  70.         $mh_pinterest_code .= '&media=' . $mh_img_url . '&description=';
  71.         $mh_pinterest_code .= urlencode( get_the_title() ) . '" class="pin-it-post"></a>';
  72.  
  73.         // Replace before the img tag in the new string
  74.         $mh_new = preg_replace( '/^/' , $mh_pinterest_code , $mh_old );
  75.         // After the img tag
  76.         $mh_new = preg_replace( '/$/' , '</span>' , $mh_new );
  77.  
  78.         // make the substitution
  79.         $content = str_replace( $mh_old, $mh_new , $content );
  80.         }
  81.     }
  82. return $content;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement