Skip to content

Instantly share code, notes, and snippets.

@Niq1982
Last active April 16, 2021 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niq1982/3c3ae20b95add98217e0292e74140fef to your computer and use it in GitHub Desktop.
Save Niq1982/3c3ae20b95add98217e0292e74140fef to your computer and use it in GitHub Desktop.
Social icons functionality for WordPress
<?php
/**
* Use same social icon component in single post share links
*/
$social_share_links = [
'facebook' => [
'url' => 'https://www.facebook.com/sharer/sharer.php?u=' . get_permalink(),
'title' => 'Jaa artikkeli Facebookissa',
],
'twitter' => [
'url' => 'https://twitter.com/share?url=' . get_permalink() . '&text=' . get_the_excerpt(),
'title' => 'Jaa artikkeli Twitterissä',
],
'linkedin' => [
'url' => 'https://www.linkedin.com/sharing/share-offsite/?url=' . get_permalink(),
'title' => 'Yleinen: Jaa artikkeli Linkedinissä',
]
];
get_template_part( 'template-parts/social-icons', '', [ 'social_icons' => $social_share_links ] );
<?php
/**
* Social icons
*
* @Author: Niku Hietanen
* @Date: 2020-11-11 11:33:59
* @Last Modified by: Niku Hietanen
* @Last Modified time: 2020-11-11 12:12:52
* @package air-light
*
* Example $social_icons array:
* [
* 'facebook' => [
* 'title' => 'Tutustu Duden facebookiin',
* 'url' => 'https://facebook.com/digitoimistodude',
* ],
* 'twitter' => [
* 'title' => 'Seuraa Dudea twitterissä',
* 'url' => 'https://twitter.com/digitoimistodude',
* ],
* ]
*/
$social_icons = $args && isset( $args['social_icons'] ) ? $args['social_icons'] : [];
$icons = [
'facebook' => 'svg/icon-facebook.svg',
'twitter' => 'svg/icon-twitter.svg',
'linkedin' => 'svg/linkedin.svg',
'instagram' => 'svg/icon-instagram.svg',
];
if ( empty( $social_icons ) ) {
return;
}
?>
<ul class="social-icons">
<?php foreach ( $social_icons as $slug => $social_icon ) : ?>
<?php // Check that the icon file exists
if ( ! isset( $icons[ $slug ] ) || ! file_exists( get_theme_file_path( $icons[ $slug ] ) ) ) {
trigger_error( 'Social icon for ' . esc_html( $slug ) . ' missing' ); // phpcs:ignore
continue;
}
?>
<li class="social-icon social-icon-<?php echo esc_attr( $slug ); ?>">
<a href="<?php echo esc_url( $social_icon['url'] ); ?>" class="no-external-link-indicator">
<span class="screen-reader-text">
<?php if ( ! empty( $social_icon['title'] ) ) {
echo esc_html( $social_icon['title'] );
} else {
echo esc_html( ucfirst( $slug ) );
} ?>
</span>
<?php include get_theme_file_path( $icons[ $slug ] ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment