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;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;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;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;amp;url=<?php the_permalink(); ?>&amp;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;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;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?

  • Your PHP is badly broken. You need to be using opening and closing PHP tags properly.
    – s_ha_dum
    May 5 '13 at 15:34

There are a couple of problems with your code:

  1. You have to close the PHP context, if you want to output plain HTML: function foo(){ ?><strong><?php }
  2. Don’t repeat yourself. Always store repeating values in variables or functions. Writing <a href more than once is a bug.
  3. Do not use get_the_title() in attributes. Use the_title_attribute( array ( 'echo' => FALSE ) ) instead, or you can get unexpected markup into your HTML output.
  4. Do not use bloginfo('stylesheet_directory'). Use get_stylesheet_directory_uri() in child themes only and get_template_directory_uri() in all other cases.
  5. Do not double-escape the &.
  6. 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&amp;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&amp;bkmk=%2$s&amp;t=%2$s',
            'text' => 'Google+1.'
        'stumbleupon' => array (
            'url'  => 'http://www.stumbleupon.com/submit?url=%1$s&amp;title=%2$s',
            'text' => 'StumbleUpon.'
        'digg' => array (
            'url'  => 'http://digg.com/submit?phase=2&amp;url=%1$s&amp;title=%2$s',
            'text' => 'Digg this!'
        'delicious' => array (
            'url'  => 'https://delicious.com/post?url=%1$s&amp;title=%2$s',
            'text' => 'Bookmark on Delicious.'
        'gmail' => array (
            'url'  => 'https://mail.google.com/mail/?view=cm&amp;fs=1&amp;to&amp;su=%1$s&amp;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 );

            '<a href="%1$s" title="%2$s"><img src="%3$s" alt="%2$s" /></a>',
            esc_attr( $service['text'] ),

Now you can call this function in a template:


First of all, you never closed your php tags.

Second of all, do not call functions each time for each social icon. Save values in the variables instead to speed up the process.

It would go something like this:

<?php function my_social_buttons() {

    $title = esc_attr( get_the_title() );
    $permalink = esc_attr( get_the_permalink() );
    $directory = get_template_directory_uri();

    <strong>Deel die pos met ander</strong>
    <a href="http://www.facebook.com/sharer.php?u=<?php echo $permalink;?>&amp;amp;t=<?php echo $title; ?>" title="Share on Facebook.">
    <img src="<?php echo $directory; ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" /></a>
    <a href="http://twitter.com/home/?status=<?php echo $title; ?> : <?php echo $permalink; ?>" title="Tweet this!">
    <img src="<?php echo $directory; ?>/images/twitter.png" alt="Tweet this!" /></a>
    <a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=<?php echo $permalink;?>&amp;amp;t=<?php echo $title; ?>" title="Google+1.">
    <img src="<?php echo $directory; ?>/images/google.png" alt="Google+1" id="Google+1" /></a>
    <a href="http://www.stumbleupon.com/submit?url=<?php echo $permalink; ?>&amp;amp;title=<?php echo $title; ?>" title="StumbleUpon.">
    <img src="<?php echo $directory; ?>/images/stumbleupon.png" alt="StumbleUpon" /></a>
    <a href="http://digg.com/submit?phase=2&amp;amp;url=<?php echo $permalink; ?>&amp;amp;title=<?php echo $title; ?>" title="Digg this!">
    <img src="<?php echo $directory; ?>/images/digg.png" alt="Digg This!" /></a>               
    <a href="http://del.icio.us/post?url=<?php echo $permalink; ?>&amp;amp;title=<?php echo $title; ?>" title="Bookmark on Delicious.">
    <img src="<?php echo $directory; ?>/images/delicious.png" alt="Boekmerk op Delicious" /></a>
    <a href="https://mail.google.com/mail/?view=cm&fs=1&to&su=<?php echo $permalink;?>&amp;amp;t=<?php echo $title; ?>" title="epos.">
    <img src="<?php echo $directory; ?>/images/gmail.png" alt="epos" id="epos dit" /></a> 


  • not working, still getting parse error May 5 '13 at 15:49
  • Can you copy the exact error please. May 5 '13 at 15:53
  • here is the error: Parse error: parse error in C:\Documents and Settings\Pieter\Desktop\xampplite\htdocs\wordpress\wp-content\themes\pietergoosen\funksies\entry-meta.php on line 109 May 5 '13 at 16:07
  • sorry, got it working, my stupid mistake, removed the opening php tag at the start. many thanks May 5 '13 at 16:10
  • I am glad you got it working. Don't forget to accept the answer :) May 5 '13 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.