Pinterest Integration Using functions.php

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.

Read More

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="http://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="http://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

Related posts

Leave a Reply

3 comments

  1. 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="http://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="http://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);
        }
    }
    
  2. You are building the URL Wrong since first it needs to be urlencoded and 2nd what is get_thumbnail() does it print the full img tag? and 3rd you are echoing it out.

    anyway try this

    /* Pinterest */
    $baseurl = 'http://pinterest.com/pin/create/button/?';
    $url = 'url='.urlencode(get_permalink());
    $mediaUrl = '&media='.urlencode(wp_get_attachment_image_src ( get_post_thumbnail_id()));
    $description = '&description='.urlencode(get_the_title());
    $fullURL = $baseurl.$url.$mediaUrl.$description;
    $content .= '<div class="pinterest-it-button">
    <a href="'.$fullURL.'" class="pin-it-button" count-layout="horizontal">Pin It</a>
            <script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>
    </div>';
    
  3. Your issue is simple php syntax. You can’t use opening and closing php braces <?php and ?> within variable assignment and separate strings and variables must be concatenated by a dot .

    Follow the G+ and Facebook buttons as examples. I also assume you want to return your results?