I want to add social share buttons to my posts, but I don’t want to put all that big bulk of code into all my content files. I want to create a function, and then add the function name to my template files, something like this:
function pietergoosen_sosiale_netwerk_deel_knoppies() {
<strong>Deel die pos met ander</strong><p </p>
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" title="Share on Facebook.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" /></a>
<a href="http://twitter.com/home/?status=<?php the_title(); ?> : <?php the_permalink(); ?>" title="Tweet this!">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/twitter.png" alt="Tweet this!" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" title="Google+1.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/google.png" alt="Google+1" id="Google+1" /></a>
<a href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="StumbleUpon.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/stumbleupon.png" alt="StumbleUpon" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="Digg this!">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/digg.png" alt="Digg This!" /></a>
<a href="http://del.icio.us/post?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="Bookmark on Delicious.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/delicious.png" alt="Boekmerk op Delicious" /></a>
<a href="https://mail.google.com/mail/?view=cm&fs=1&to&su=<?php the_permalink();?>&amp;t=<?php the_title(); ?>" title="epos.">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/gmail.png" alt="epos" id="epos dit" /></a> }
This isn’t working. How should I do it correctly?
2 Answers
There are a couple of problems with your code:
- You have to close the PHP context, if you want to output plain HTML:
function foo(){ ?><strong><?php }
- Don’t repeat yourself. Always store repeating values in variables or functions. Writing
<a href
more than once is a bug. - Do not use
get_the_title()
in attributes. Usethe_title_attribute( array ( 'echo' => FALSE ) )
instead, or you can get unexpected markup into your HTML output. - Do not use
bloginfo('stylesheet_directory')
. Useget_stylesheet_directory_uri()
in child themes only andget_template_directory_uri()
in all other cases. - Do not double-escape the
&
. - The share URL for Delicious is
https://delicious.com/post?
. It has been that for years.
The resulting code could look like this:
function pietergoosen_sosiale_netwerk_deel_knoppies()
{
$services = array (
'facebook' => array (
'url' => 'http://www.facebook.com/sharer.php?u=%1$s&t=%2$s',
'text' => 'Share on Facebook.'
),
'twitter' => array (
'url' => 'http://twitter.com/home/?status=%1$s%%20-%%20%2$s',
'text' => 'Tweet this!'
),
'google' => array (
'url' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk=%2$s&t=%2$s',
'text' => 'Google+1.'
),
'stumbleupon' => array (
'url' => 'http://www.stumbleupon.com/submit?url=%1$s&title=%2$s',
'text' => 'StumbleUpon.'
),
'digg' => array (
'url' => 'http://digg.com/submit?phase=2&url=%1$s&title=%2$s',
'text' => 'Digg this!'
),
'delicious' => array (
'url' => 'https://delicious.com/post?url=%1$s&title=%2$s',
'text' => 'Bookmark on Delicious.'
),
'gmail' => array (
'url' => 'https://mail.google.com/mail/?view=cm&fs=1&to&su=%1$s&t=%2$s',
'text' => 'Share per Gmail.'
)
);
$img_base = get_template_directory_uri() . '/images/%s.png';
$title = the_title_attribute( array ( 'echo' => FALSE ) );
$url = urlencode( get_permalink() );
print '<h4>Deel die pos met ander</h4>';
foreach ( $services as $name => $service )
{
$href = sprintf( $service['url'], $url, urlencode( $title ) );
$src = sprintf( $img_base, $name );
printf(
'<a href="https://wordpress.stackexchange.com/questions/98392/%1$s" title="%2$s"><img src="%3$s" alt="%2$s" /></a>',
$href,
esc_attr( $service['text'] ),
$src
);
}
}
Now you can call this function in a template:
pietergoosen_sosiale_netwerk_deel_knoppies();