Skip to content

Instantly share code, notes, and snippets.

@Camwyn
Last active August 29, 2015 14:17
Show Gist options
  • Save Camwyn/1815927a236408cda965 to your computer and use it in GitHub Desktop.
Save Camwyn/1815927a236408cda965 to your computer and use it in GitHub Desktop.
Function to build social icon list for wordpress
<?php
/**
* Output social icons for a post.
* Assumes you have filters for building share URLs named build_twitter_url & etc.
* @param integer $post_id: the id of the post
* @param string $classes: a space-separated list of classes to add to the section
* @param boolean $circled: use circled icons
* @return string HTML section containing the list of icon links
*/
public function social_icons( $post_id = NULL, $classes = '', $circled = FALSE )
{
//for showing more than one on a page
static $output_id = 0;
$output_id++;
//Make sure we have the post ID to reference when sharing
if ( ! $post_id )
{
$post_id = get_post()->ID;
}//end if
//List of social networks we want icons for
$social_networks = array(
'twitter',
'facebook',
'linkedin',
'reddit',
'googleplus',
'email'
);
?>
<section id="post-header-post-social-<?php echo absint( $output_id ); ?>"
class="post-social <?php echo esc_attr( $classes ); ?>">
<ul class="social">
<?php
foreach( $social_networks as $network ) {
?>
<li class="<?php echo esc_attr( $network ); ?>">
<a href="<?php echo esc_url( apply_filters( 'build_' . $network . '_url', $post_id ) ); ?>"
title="Share on <?php echo esc_attr( ucfirst( $network ) ); ?>"
class="icon-<?php echo esc_attr( $network ); ?><?php if( $circled ) { echo '-circled'; } ?>"></a>
</li>
<?php
}//end foreach
?>
</ul>
</section>
<?php
}//end social_icons
?>
@Camwyn
Copy link
Author

Camwyn commented Mar 19, 2015

Created as a sample.
Assumes WordPress.
Assumes there are filters in place for url building.
Assumes two types of icons - "round" and another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment