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();