jan_dembowski

more-goofy-code.php

Jan 8th, 2013
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 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.2
  8.  
  9. From http://wordpress.org/support/topic/add-code-before-each-image
  10.  
  11.  
  12. <style type="text/css">
  13. span.pinterest-button { position: relative; width: 100%; float: left; clear: both; margin: 0; }
  14. .pin-it { display:block; width: 100px; height: 100px; background:url('http://www.intimateweddings.com/blog/wp-content/uploads/2012/06/pinitbutton.png'); z-index:10; bottom: 25px; right: 250px; position:absolute; }
  15. span.pinterest-button .pin-it { display:none; }
  16. span.pinterest-button:hover .pin-it { display:block; }
  17. </style>
  18.  
  19. http://assets.pinterest.com/images/PinExt.png
  20.  
  21. */
  22.  
  23. add_filter( 'the_content' , 'mh_wrap_image' , 15 );
  24.  
  25. function mh_wrap_image( $content ) {
  26.  
  27. // Regex to find all <img ... > tags
  28. $mh_img_regex1 = "/\<img [^>]*src=\"([^\"]+)\"[^>]*>/";
  29.  
  30. // Regex to find all <a href"..."><img ... ></a> tags
  31. $mh_img_regex2 = "/<a href=.*><img [^>]*src=\"([^\"]+)\"[^>]*><\/a>/";
  32.  
  33. // Populate the results into 2 arrays
  34. preg_match_all( $mh_img_regex1 , $content, $mh_img );
  35. preg_match_all( $mh_img_regex2 , $content, $mh_matches );
  36.  
  37. // The second array will be a subset of the first so go through
  38. // each element and delete the duplicates in the first.
  39. $i=0;
  40. foreach ( $mh_img[0] as $mh_img_count ) {
  41.     $i2=0;
  42.     foreach ( $mh_matches[0] as $mh_matches_count ) {
  43.         if ( strpos($mh_matches_count, $mh_img_count ) ){
  44.             unset( $mh_img[0][$i] );
  45.             unset( $mh_img[1][$i] );
  46.                 $i2++;
  47.             break;
  48.         }
  49.             $i2++;
  50.             }
  51.     $i++;
  52.     }
  53. // There is almost certainly a better way to do this.
  54. // Append the no links array to the $mh_matches array.
  55. $i=0;
  56. $mh_start = count( $mh_matches[0] );
  57. foreach ( $mh_img[0] as $mh_img_count ) {
  58.     $mh_matches[0][ $mh_start + $i ] = $mh_img_count;
  59.     $i++;
  60. }
  61. $i=0;
  62. foreach ( $mh_img[1] as $mh_img_count ) {
  63.     $mh_matches[1][ $mh_start + $i ] = $mh_img_count;
  64.     $i++;
  65. }
  66.  
  67. // If we get any hits then put the code before and after the img tags
  68. if ( $mh_matches ) {;
  69.     for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
  70.         {
  71.         // Old img tag
  72.         $mh_old = $mh_matches[0][$mh_count];
  73.  
  74.         // Get the img URL, it's needed for the button code
  75.         $mh_img_url = $mh_matches[1][$mh_count];
  76.  
  77.         // Put together the pinterest code to place before the img tag
  78.         $mh_pinterest_code = '<span class="pinterest-button"><a href="http://pinterest.com/';
  79.         $mh_pinterest_code .= 'pin/create/button/?url=' . urlencode( get_permalink() );
  80.         $mh_pinterest_code .= '&media=' . $mh_img_url . '&description=';
  81.         $mh_pinterest_code .= urlencode( get_the_title() ) . '" class="pin-it"></a>';
  82.  
  83.         // Replace before the img tag in the new string
  84.         $mh_new = preg_replace( '/^/' , $mh_pinterest_code , $mh_old );
  85.         // After the img tag
  86.         $mh_new = preg_replace( '/$/' , '</span>' , $mh_new );
  87.  
  88.         // make the substitution
  89.         $content = str_replace( $mh_old, $mh_new , $content );
  90.         }
  91.     }
  92. return $content;
  93. }
  94.  
  95. add_action( 'wp_head' , 'mh_pinit_button' );
  96.  
  97. function mh_pinit_button() {
  98. ?>
  99.  
  100. <style type="text/css">
  101. span.pinterest-button {
  102.     position: relative;
  103.     width: 273px;
  104.     clear: both;
  105.     margin: 0;
  106. }
  107. .pin-it {
  108.     display:block;
  109.     width: 43px;
  110.     height: 21px;
  111.     background:url('<?php echo plugin_dir_url( __FILE__ ) . 'PinExt.png'; ?>');
  112.     z-index:10;
  113.     top: 50px;
  114.     left: 50px;
  115.     position:absolute;
  116. }
  117. span.pinterest-button .pin-it {
  118.     display:none;
  119. }
  120. span.pinterest-button:hover .pin-it {
  121.     display:block;
  122. }
  123. </style>
  124.  
  125. <?php }
Advertisement
Add Comment
Please, Sign In to add comment