0

I'm trying to create a shortcode to insert the Pinterest button with variables manually in posts. This is the code I've arrived at after reading various posts and grabbing the code from Pinterest. I haven't tested it yet. I wondered whether someone would kindly check for any obvious errors or problems?

Secondly, I wondered if in place of the 'url' variable the url / permalink of the post can be grabbed automatically? So this is the code I have.

Any thoughts / comments?...

function mypinfunction( $atts ) {

extract( shortcode_atts( array(
    'url' => ''
    'img' => ''
    'description' => ''
), $atts ) );

return '<span class="lowpin"><a data-pin-config="none"
href="//pinterest.com/pin/create/button/
url='.url.'&media='.img.'&description='.description.'" data-pin-do="buttonPin" >
<img src="//assets.pinterest.com/images/pidgets/pin_it_button.png" /></a></span>'

add_shortcode('skipspin', 'mypinfunction');

The short code to insert would be [skipspin url="http://example.com" img="http://example.com/image.jpg" description="some words here"]

1 Answer 1

0

If you were to enable debugging and test the code (not sure why you are reluctant to do that), you would see that there are problems with the code. Your array elements need to be separated by commas and PHP variables begin with $. What you have won't work as is. That kind of pure PHP code review is off-topic here. Perhaps post on the Code Review sister site for that, but test the code yourself first.

The only WordPress specific part of the question is:

Secondly, I wondered if in place of the 'url' variable the url / permalink of the post can be grabbed automatically?

Yes. Inside a Loop, this will give you the post permalink.

global $post;
$url = get_permalink($post);

This is closer to correct.

function mypinfunction( $atts ) {
  global $post;
  extract( shortcode_atts( array(
      'url' => '',
      'img' => '',
      'description' => ''
  ), $atts ) );

  $url = get_permalink($post);

  return '<span class="lowpin"><a data-pin-config="none"
  href="//pinterest.com/pin/create/button/
  url='.$url.'&media='.$img.'&description='.$description.'" data-pin-do="buttonPin" >
  <img src="//assets.pinterest.com/images/pidgets/pin_it_button.png" /></a></span>';
}
add_shortcode('skipspin', 'mypinfunction');
4
  • Now implement add_query_arg() and esc_url(), and we are on topic. :)
    – fuxia
    Mar 7, 2013 at 23:16
  • OK, thank you both. I wasn't feeling very confident so thought a trained eye would spot any glaring problems which you have so I'm really grateful. Have used this site before and Google Search brought me here pretty directly. Apologies if I'm off topic but I'm going to pass some of the blame to Google for that one + also answers to questions posted on this site previously which touched on this :) ... thanks again. @Toscho I'll take a look at those thanks.
    – Paul Brown
    Mar 8, 2013 at 0:45
  • Thanks for this, I've tested the code after a deeper look following your comments and quite shocked how close I got! Works fine. Need to play around with your suggestions re capturing URL because want to do that outside the loop (i.e. just editing functions file not theme files). For now manual input works fine. Thanks again.
    – Paul Brown
    Mar 8, 2013 at 1:20
  • Just for completeness a quick update. The code suggested by @s_ha_dum works perfectly for me. Anyone else trying to implement should add a ? at the end of the second line of the return code (I deleted it by mistake) - but the Pinterest code may change so cross refer there. I also added $description = get_the_title($post); So now the only variable I need to manually input is the image URL so the shortcode looks [skipspin img="http://example.com/myimg.jpg] Thank you once again.
    – Paul Brown
    Mar 8, 2013 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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