Skip to content

Instantly share code, notes, and snippets.

@seezee
Last active January 4, 2022 16:01
Show Gist options
  • Save seezee/9a4760f9ff3725fb831207ff09f36bb0 to your computer and use it in GitHub Desktop.
Save seezee/9a4760f9ff3725fb831207ff09f36bb0 to your computer and use it in GitHub Desktop.
Filter to add lightweight social sharing to WP Genesis themes. Include via functions.php.
<?php
/**
* Social sharing buttons meta to Genesis.
*
* @package There is no package.
* @since 1.0.0
*/
// Security.
if ( ! defined( 'ABSPATH' ) ) {
die( 'Sorry, you are not allowed to access this page directly.' );
}
/**
* Add social sharing to Genesis entry meta header.
*/
function my_theme_post_info_filter( $post_info ) {
global $post;
$arr = array(); // Use this with wp_kses. Don't allow any HTML.
if ( is_singular() || is_home() ) {
// Get current page URL.
$myThemeURL = urlencode( get_permalink() );
// Get current page title.
$myThemeTitle = htmlspecialchars( rawurlencode( html_entity_decode( get_the_title(), ENT_COMPAT, 'UTF-8' ) ), ENT_COMPAT, 'UTF-8' );
$t_user = genesis_get_option( 'my_theme_twitter', my_theme_SETTINGS_FIELD );
// Construct sharing URL without using any script.
$twitterURL = '//twitter.com/intent/tweet?text=' . $myThemeTitle . '&amp;url=' . $myThemeURL . '&amp;via=' . $t_user;
$facebookURL = '//www.facebook.com/sharer/sharer.php?u=' . $myThemeURL;
// This is for the US Central time zone. Adjust as needed for your location.
$tz = get_the_modified_time( 'T' );
if ( 'CST' === $tz ) {
$ltz = '-6:00';
} elseif ( 'CDT' === $tz ) {
$ltz = '-5:00';
} else {
$ltz = '';
}
$post_info = '<span class="red"><i>by</i> <i class="fas fa-fw fa-user"></i> [post_author_posts_link] <i>on</i> <i class="fas fa-fw fa-calendar-alt"></i>[post_date]</span> &mdash; <span class="entry__meta--no-print"><small><i class="fas fa-fw fa-comments"></i> [post_comments zero="Leave a Comment" one="Read 1 Comment" more="Read % Comments"] or Share:</small>&nbsp;<span class="mpb-social"><a class="mpb-social-link mpb-twitter" href="' . wp_kses( $twitterURL, $arr ) . '" target="_blank" rel="external noopener noreferrer"><i class="fab fa-2x fa-twitter-square"></i> <span class="hide">Tweet this post</span></a>&nbsp;<a class="mpb-social-link mpb-facebook" href="' . wp_kses( $facebookURL, $arr ) . '" target="_blank" rel="external noopener noreferrer"><i class="fab fa-2x fa-facebook-square"></i> <span class="hide">Share this post on Facebook</span></a></span> [post_edit]</span><br /><small><i>Latest revision:</i> <time datetime="' . get_the_modified_time( 'Y-m-d\TG:i:s', true, null, true ) . $ltz . '">' . get_the_modified_date() . '</time></small>';
return $post_info;
} else {
return $post_info;
}
}
add_filter( 'genesis_post_info', 'my_theme_post_info_filter' );
@seezee
Copy link
Author

seezee commented Jan 4, 2022

2022-01-04 Added 'rel="external noopener noreferrer"' to external links for XSS security.

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