2

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="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

2
  • Sorry, english isn´t my first language. This code is from functions.php, without using an plugin. I think it´s a problem of the differnt quotes ' "
    – ogni
    Jul 9, 2012 at 13:43
  • @ogni No offense. By plugins <=> function(s), I meant to say that a plugin basically constitutes one or more functions. So, you should NOT really worry about using plugins. What you should worry about is, finding a well coded and well maintained plugin. :)
    – its_me
    Jul 9, 2012 at 14:23

3 Answers 3

4

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
  • +1 : Kudos for the creativity and text interpretation! Much better than repairing a not working and incomplete code: very nice sample of clean, simple and hooked Answer :)
    – brasofilo
    Jul 9, 2012 at 16:09
  • @ogni : the Way of the Stack is : instead of saying "thanks", press the up-vote button :))
    – brasofilo
    Jul 9, 2012 at 16:22
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
  • @Thanks Bainternet: '<a href="'.$fullURL.'" class="pin-it-button" count-layout="horizontal">Pin It</a> 'Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /functions.php on line 265'
    – ogni
    Jul 9, 2012 at 14:53
  • 1
    @Bainternet - you used the_title() inside the $description variable. Jul 9, 2012 at 15:10
  • @EvanMattson nice catch, as for ogni there is no syntax error with the above code, create a pastebin of your functions.php file and I'll take a look.
    – Bainternet
    Jul 9, 2012 at 15:53
0

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.