Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm coding my first WP shortcode and am having trouble turning my working PHP into a working shortcode. I have successfully created a basic shortcode that returns text in the spot I'd like so that much is good. But I'm not doing so well putting the chocolate in the peanut butter.

Here's what I have for the working PHP:

<div class="authorsocial">
    <h5>Connect, Share &amp; Follow</h5>
        <ul>
           <?php $meta_socialgr = apply_filters('the_content', get_post_meta($post->ID, 'author_goodreads', true));
              if (!empty($meta_socialgr)) { ?>
                <li><a href="<?php echo get_post_meta( $post->ID, 'author_goodreads', true );?>"><img src="http://fiddlehead.milkbossindustries.com/files/goodreads.jpg"></a></li>
           <?php } ?>
        </ul>
</div><!-- end authorsocial -->              

And here is my basic shortcode:

<?php
// Adding a shortcode to return each author's social media links
    function funcauthor_social( $atts ){
        $output = 'bubbalicious';
        return $output;
    }
    add_shortcode( 'author_social', 'funcauthor_social' );
?>

I'm not having much success in replacing "bubbalicious" with my functions as I'm just not grasping...something.

<?php
// Adding a shortcode to return each author's social media links
    function funcauthor_social( $atts ){
        $output = '<div class="authorsocial">
        <h5>Connect, Share &amp; Follow</h5>
        <ul>'.$meta_socialgr = apply_filters('the_content', get_post_meta($post->ID, 'author_goodreads', true));
             if (!empty($meta_socialgr)) { '
             <li><a href="'.echo get_post_meta( $post->ID, 'author_goodreads', true );.'"><img src="http://fiddlehead.milkbossindustries.com/files/goodreads.jpg"></a></li>'.}.'
        </ul>
      </div>';
      return $output;
 }
    add_shortcode( 'author_social', 'funcauthor_social' );
    ?>
share|improve this question
add comment

migrated from stackoverflow.com May 11 '12 at 1:29

This question came from our site for professional and enthusiast programmers.

1 Answer

Separate out your logic from the string you want to return:

<?php
// Adding a shortcode to return each author's social media links
    function funcauthor_social( $atts ){
        $meta_socialgr = esc_url(get_post_meta( $post->ID, 'author_goodreads', true ));

        $output = '<div class="authorsocial"><h5>Connect, Share &amp; Follow</h5><ul>';

        if (!empty($meta_socialgr)) { 
            $output .= '<li><a href="'.$meta_socialgr.'"><img src="http://fiddlehead.milkbossindustries.com/files/goodreads.jpg"></a></li>';
          }

         $output .= '</ul></div>';

        return $output;
 }
    add_shortcode( 'author_social', 'funcauthor_social' );
    ?>

It's not clear why you are applying the_content filter to $meta_socialgr which looks like it should be an url...?

Also, when displaying data from the database (or otherwise some user input) make sure you sanitize that data.

share|improve this answer
    
Thank you Stephen. I can read through your solution and understand it (mostly, except the $url = esc_attr line). I removed the filter. However, when I implement the above, the h5 displays but the if statement does not return the image nor link. There is a url in the custom field and the field name matches the code. Thoughts anyone? –  djw May 11 '12 at 4:06
    
@djw - see updated answer :). You wanted to keep the get_post_meta call, but not apply the filter to it. The esc_attr sanitises the input for use in attributes in HTML (like 'value' for inputs, or element classes etc.). Actually esc_url is more appropriate here, and I've updated the answer to use that instead. –  Stephen Harris May 11 '12 at 12:37
    
Thank you. I did manage to properly remove the filter so thank you for pointing that out. And thank you for explaining esc_url better than the google. I appreciate your help. Learned a bunch! Will keep troubleshooting why it won't work in my implementation as I actually understand what you've done here. –  djw May 13 '12 at 20:56
    
Got it. Needed global $post; in there after function funcauthor_social( $atts ){ which I suppose was what I was trying to accomplish with the misguided filter. A little knowledge can be a dangerous thing. :) –  djw May 14 '12 at 22:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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