Creating a WordPress shortcode

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.

Leave a Comment