Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

create a shortcode from my social php options WordPress

  • SOLVED

I do not enough about php to do this. I need a function that I will place in my functions file and just drop a shortcode into the editor for a post or page,
<?php if(of_get_option('footer_social', '1')) {?>
<?php
echo '<ul class="social social-inline">';
if (of_get_option('twitter_url')) echo '<li><a href="' . of_get_option('twitter_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/twitter.png" alt="vist us on twitter" /></a></li>';

if (of_get_option('fb_url')) echo '<li><a href="' . of_get_option('fb_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/facebook.png" alt="vist us on Facebook" /></a></li>';

if (of_get_option('pinterest_url')) echo '<li><a href="' . of_get_option('pinterest_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/pinterest.png" alt="vist us on Pinterest" /></a></li>';

if (of_get_option('linkedin_url')) echo '<li><a href="' . of_get_option('linkedin_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/linkedin.png" alt="vist us on Linkedin" /></a></li>';

if (of_get_option('google_url')) echo '<li><a href="' . of_get_option('google_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/google.png" alt="vist us at Google" /></a></li>';

if (of_get_option('github_url')) echo '<li><a href="' . of_get_option('github_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/github.png" alt="vist us at Github" /></a></li>';

if (of_get_option('rss_url')) echo '<li><a href="' . of_get_option('rss_url') . '"><img src="'
. get_template_directory_uri().'/assets/social/rss.png" alt="Our rss feed" /></a></li>';

echo '</ul><!-- end of .social-icons -->';
?>
<?php } ?>


Answers (2)

2013-03-15

Kyle answers:

Function isn't too hard, but when echoing in shortocodes you need to use the output buffer

function mysp_function(){
if(of_get_option('footer_social', '1')) {

ob_start();
?>

<?php

echo '<ul class="social social-inline">';

if (of_get_option('twitter_url')) echo '<li><a href="' . of_get_option('twitter_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/twitter.png" alt="vist us on twitter" /></a></li>';



if (of_get_option('fb_url')) echo '<li><a href="' . of_get_option('fb_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/facebook.png" alt="vist us on Facebook" /></a></li>';



if (of_get_option('pinterest_url')) echo '<li><a href="' . of_get_option('pinterest_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/pinterest.png" alt="vist us on Pinterest" /></a></li>';



if (of_get_option('linkedin_url')) echo '<li><a href="' . of_get_option('linkedin_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/linkedin.png" alt="vist us on Linkedin" /></a></li>';



if (of_get_option('google_url')) echo '<li><a href="' . of_get_option('google_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/google.png" alt="vist us at Google" /></a></li>';



if (of_get_option('github_url')) echo '<li><a href="' . of_get_option('github_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/github.png" alt="vist us at Github" /></a></li>';



if (of_get_option('rss_url')) echo '<li><a href="' . of_get_option('rss_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/rss.png" alt="Our rss feed" /></a></li>';



echo '</ul><!-- end of .social-icons -->';

?>

<?php
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}

}

add_shortcode('my_social_php','mysp_function');


Shortcode is [my_social_php]


Rick Bible comments:

did not crash but nothing showed up as far as social icons. I need time to look at it a bit


Rick Bible comments:

shortcode insert would be just [my_social_php] correct?


Kyle comments:

Have you used the code in a template to confirm it works?


Kyle comments:

Yes


Rick Bible comments:

I am going to do do_shortcode and see


Kyle comments:

I meant have you put that code chunk itself into a template and verified that it outputs what you want ( to be sure the shortcode can work )


Rick Bible comments:

I had to try your code out on a theme I have that is clean. It works fine. I have a client who bought a theme that sucks, I mean, it's horrible, and I have found many things that just do not work. So thanks, and I can use this code on other projects

2013-03-15

Arnav Joy answers:

use this

<?php

add_shortcode('my_social', 'my_social');

function my_social($atts, $content="") {

if(of_get_option('footer_social', '1')) {


$str = '<ul class="social social-inline">';

if (of_get_option('twitter_url')) $str .= '<li><a href="' . of_get_option('twitter_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/twitter.png" alt="vist us on twitter" /></a></li>';



if (of_get_option('fb_url')) $str .= '<li><a href="' . of_get_option('fb_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/facebook.png" alt="vist us on Facebook" /></a></li>';



if (of_get_option('pinterest_url')) $str .= '<li><a href="' . of_get_option('pinterest_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/pinterest.png" alt="vist us on Pinterest" /></a></li>';



if (of_get_option('linkedin_url')) $str .= '<li><a href="' . of_get_option('linkedin_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/linkedin.png" alt="vist us on Linkedin" /></a></li>';



if (of_get_option('google_url')) $str .= '<li><a href="' . of_get_option('google_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/google.png" alt="vist us at Google" /></a></li>';



if (of_get_option('github_url')) $str .= '<li><a href="' . of_get_option('github_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/github.png" alt="vist us at Github" /></a></li>';



if (of_get_option('rss_url')) $str .= '<li><a href="' . of_get_option('rss_url') . '"><img src="'

. get_template_directory_uri().'/assets/social/rss.png" alt="Our rss feed" /></a></li>';



$str .= '</ul><!-- end of .social-icons -->';

} ?>

<?php return $str;} ?>


and new shortcode will be

[my_social]


Rick Bible comments:

page went white,


Arnav Joy comments:

show me your functions.php