I use a theme to build an portfolio.

I´d like to add an pinterest “pin-it button” into the fuctions.php:
Facebook, Google+ an Twitter were done, just the pinterest won´t work.

Can´t found the the wrong code.
This ist my code:

function share_this($content){
if ( is_singular( 'portfolio' ) ) {
$content .= '<div class="share-this">' .  
      /* Facebook */
      '<div class="facebook-like-button">' . 
            '<iframe src="https://www.facebook.com/plugins/like.php?href=". urlencode( get_permalink( $post->ID ) ) . 
            "&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;colorscheme=light&amp;height=21"' . 
            'scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowTransparency="true"></iframe>' .
      '</div>' . 

     /* Pinterest */
    '<div class="pinterest-it-button">' 
             '<a href="http://pinterest.com/pin/create/button/?url=<?php echo get_permalink(); ?>&media=<?php echo get_thumbnail(); ?>
             '&description=<?php the_title(); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>
            '<script type="text/javascript" src="https://assets.pinterest.com/js/pinit.js"></script>'.
    '</div>' . 

     /* Googgle+ */
    '<div class="plusone"><g:plusone size="medium" href="'.get_permalink().'"></g:plusone></div>' .
     /* Twitter */
    '<div><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a></div>' .

'</div>';
}

Thanks

3 Answers
3

There were several errors in your code. The biggest was that the function didn’t even have a closing bracket, but we can probably assume that was just not copied into your post, otherwise nothing would have been working for you.

This is a filter on the content right?

It looks like you want to append these shares after your post content.
If that’s the case than you would need this line as well:

add_filter('the_content','share_this');

Other than that, you were trying to close php tags and open them in your statement where you are appending string data to the $content variable. You were also trying to echo data and used functions to echo instead of return (for example the_title echos instead of get_the_title which returns the value).

Instead of one big string appended to your content, I cleaned things up using an array-building method, but multiple $shares .= would have worked too.

This should work although I have not tested it.

<?php
function share_this($content) {
    if ( is_singular( 'portfolio' ) ) {
        global $post;

        $link = esc_attr(get_permalink($post->ID));
        $title = esc_attr(get_the_title($post->ID));

        $image_id = get_post_thumbnail_id($post-ID);
        $image_url = wp_get_attachment_image_src($image_id);
        $thumb = $image_url[0];

        $shares = array();
        $shares[] = '<div class="share-this">';

        /* Facebook */
        $shares[] = '<div class="facebook-like-button"><iframe src="https://www.facebook.com/plugins/like.php?href=".$link."&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowTransparency="true"></iframe></div>';

        /* Pinterest */
        $shares[] = '<div class="pinterest-it-button"><a href="http://pinterest.com/pin/create/button/?url=".$link."&amp;media=".$thumb."description='.$title.' class="pin-it-button" count-layout="horizontal">Pin It</a><script type="text/javascript" src="https://assets.pinterest.com/js/pinit.js"></script></div>';

        /* Googgle+ */
        $shares[] = '<div class="plusone"><g:plusone size="medium" href="'.$link.'"></g:plusone></div>';

        /* Twitter */
        $shares[] = '<div><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a></div>';

        $shares[] = '</div>';

        return $content . implode("\n", $shares);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *