How to use the_permalink() on wordpress when it’s wrapped with ‘ or “

I have a code that wraps every image on posts with an external div (for share buttons when hovering on images).
The thing is, when I want to write the_permalink(); in the function.php, it’s wrapped inside a ‘ or a ” in the a href tags.

That causes share links look like this:

Read More

https://plus.google.com/share?url=%3C?php%20the_permalink();%20?%3E

This is the code in function.php:

function breezer_addDivToImage( $content ) {
   // A regular expression of what to look for.
   $pattern = '/(<img([^>]*)>)/i';
   // What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
   $the_url = the_permalink();
   $replacement = '<div class="imgWrap"> 
                        $1
                        <div class="imgDescription">
                                            <div class="theShareLinks">
                                                <img src="http://localhost/mySite/wp-content/uploads/2014/08/dfc2.png" />
                                                <a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>
                                                <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>?" title="Share on Facebook" onclick="window.open(this.href, 'newwin', 'width=500, height=200'); return false;" class="img-facebook"></a>
                                                <a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" class="img-google" title="Share on Google"></a>
                                            </div>
                                        </div>
                    </div>';

   // run preg_replace() on the $content
   $content = preg_replace( $pattern, $replacement, $content );

   // return the processed content
   return $content;
}

add_filter( 'the_content', 'breezer_addDivToImage' );

These are the lines from the code above^ that are wrapped in the ” ” :

<a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>?" title="Share on Facebook" onclick="window.open(this.href, 'newwin', 'width=500, height=200'); return false;" class="img-facebook"></a>
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" class="img-google" title="Share on Google"></a>

Related posts

Leave a Reply

1 comment

  1. Changing:

     <a href="http://twitter.com/share?text=&url=<?php the_permalink(); ?>" class="img-twitter" title="Share on Twitter"></a>
    

    to

    <a href="http://twitter.com/share?text=&url='.get_the_permalink() .'" class="img-twitter" title="Share on Twitter"></a>
    

    should work.