Reputation: 4580
I am pretty new to php. Please excuse me if this is a foolish question. I have added social share icons to my blog via php. In the main index page I added facebook, twitter and google plus share icons and in the single post I added some more icons.
How I implemented?
I created two php files social_main_index.php and social_single_post.php
Then in the main index template I added the code <? php include("social_main_index.php") ?>
In single post template I added the code <? php include("social_single_post.php") ?>
My Question
Can I create a single php file containing both codes and call any one of them specifically?
My code in social_main_index.php
<?php include_once("custom_css.php") ?>
<div class="social-single">
<!--Facebook Share-->
<div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(get_permalink()); ?>&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
<!--Google Plus Share-->
<div id="plusonebutton"><g:plusone href="<?php echo rawurlencode(get_permalink()); ?> "size="medium"></g:plusone></div>
<!--Twitter Share-->
<div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink() ?>" data-counturl="<?php the_permalink() ?>" data-text="<?php the_title(); ?>" data-via="nucleationin" data-related="nucleationin"></a></div>
</div>
</div>
My code in social_single_post.php
<?php include_once("custom_css.php") ?>
<div class="social-single">
<!--Facebook Share-->
<div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(get_permalink()); ?>&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
<!--Google Plus Share-->
<div id="plusonebutton"><g:plusone href="<?php echo rawurlencode(get_permalink()); ?> "size="medium"></g:plusone></div>
<!--Twitter Share-->
<div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink() ?>" data-counturl="<?php the_permalink() ?>" data-text="<?php the_title(); ?>" data-via="nucleationin" data-related="nucleationin"></a></div></div>
<!--Linkedin Share-->
<div id="linkedinshare"><script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="in/share" data-url="<?php the_permalink() ?>" data-counter="right"></script></div>
<!--Stumble Share-->
<div id="stumblebutton"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1"></script></div>
</div>
Upvotes: 0
Views: 229
Reputation: 7722
This is actually a pretty simple thing to do, and I recommend you read up on the manual when you get a chance. Just create a function that will return either bit of html:
function print_buttons($button_set) {
if ($button_set == "one") { //if the local variable for the function is the string "one", then paste the data below where the function is called
return '<div class="social-single">
<!--Facebook Share-->
<div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href='.rawurlencode(get_permalink()).'&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
<!--Google Plus Share-->
<div id="plusonebutton"><g:plusone href="'.rawurlencode(get_permalink()).'"size="medium"></g:plusone></div>
<!--Twitter Share-->
<div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="'.the_permalink().'" data-counturl="'.the_permalink().'" data-text="'.the_title().'" data-via="nucleationin" data-related="nucleationin"></a></div>
</div>
</div>';
}
else { //if the local variable is anything else, do the same thing, but with the below data instead.
return '<?php include_once("custom_css.php") ?>
<div class="social-single">
<!--Facebook Share-->
<div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href='.rawurlencode(get_permalink()).'&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
<!--Google Plus Share-->
<div id="plusonebutton"><g:plusone href="'.rawurlencode(get_permalink()).'"size="medium"></g:plusone></div>
<!--Twitter Share-->
<div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="'.the_permalink().'" data-counturl="'.the_permalink().'" data-text="'.the_title().'" data-via="nucleationin" data-related="nucleationin"></a></div></div>
<!--Linkedin Share-->
<div id="linkedinshare"><script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="in/share" data-url="'.the_permalink().'" data-counter="right"></script></div>
<!--Stumble Share-->
<div id="stumblebutton"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1"></script></div>
</div>';
}
}
And then to use it...
echo print_buttons("one"); //this will print the first group
echo print_buttons("anything else"); //this will print the second
Take note that this is a shoot in the dark, as I don't know what the functions you were echoing out and otherwise returned/did. If I did, I could say for certain this would work for you. I however do not, and thus the above.
To use this function, make sure it is included on the page it is being used in, just as the css above is included via include()
. Example:
some_functions.php:
<?php
function the_function_above() { }
?>
And then...
The_page_above.php:
<?php
include("some_functions.php");
echo the_function_above();
?>
Upvotes: 2