Skip to content

Instantly share code, notes, and snippets.

@JiveDig
Last active March 28, 2018 18:32
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 JiveDig/2f7987e8a34570174363f1bc5a8291c5 to your computer and use it in GitHub Desktop.
Save JiveDig/2f7987e8a34570174363f1bc5a8291c5 to your computer and use it in GitHub Desktop.
Use Genesis hooks to display Social Warfare icons based on SW settings.
<?php
/**
* Use Genesis hooks to display Social Warfare icons based on SW settings.
*
* @version 1.1.0
*
* @author Mike Hemberger @JiveDig
*
* @return void
*/
add_action( 'genesis_before', 'prefix_social_warfare_genesis_hooks' );
function prefix_social_warfare_genesis_hooks() {
// Bail if not a single post, archive, blog, or front page.
if ( ! ( is_singular() || is_archive() || is_home() || is_front_page() ) ) {
return;
}
// Bail if Social Warfare is not active.
if ( ! function_exists( 'social_warfare' ) ) {
return;
}
// Remove default display of SW icons.
remove_filter( 'the_content','social_warfare_wrapper', 10 );
remove_filter( 'the_excerpt','social_warfare_wrapper' );
// Get global SW options.
global $swp_user_options;
// Build the single post type key in SW options.
$singular_key = sprintf( 'location_%s', get_post_type() );
// If a valid singular location in SW options.
if ( is_singular() && isset( $swp_user_options[ $singular_key ] ) ) {
// Check the display location settings and set the hook locations.
$before = in_array( $swp_user_options[ $singular_key ], array( 'above', 'both' ) );
$after = in_array( $swp_user_options[ $singular_key ], array( 'below', 'both' ) );
$before_hook = 'genesis_before_entry_content';
$after_hook = 'genesis_after_entry_content';
}
// Or if archive/blog/front page.
elseif ( is_archive() || is_home() ) {
// Check the display location settings and set the hook locations.
$before = in_array( $swp_user_options[ 'locationSite' ], array( 'above', 'both' ) );
$after = in_array( $swp_user_options[ 'locationSite' ], array( 'below', 'both' ) );
$before_hook = 'genesis_before_loop';
$after_hook = 'genesis_after_loop';
}
// If front page.
elseif ( is_front_page() ) {
// Check the display location settings and set the hook locations.
$before = in_array( $swp_user_options[ 'locationHome' ], array( 'above', 'both' ) );
$after = in_array( $swp_user_options[ 'locationHome' ], array( 'below', 'both' ) );
$before_hook = 'genesis_before_loop';
$after_hook = 'genesis_after_loop';
}
// If showing icons before content.
if ( $before ) {
// Display the SW icons.
add_action( $before_hook, function() {
social_warfare();
});
}
// If showing icons before content.
if ( $after ) {
// Display the SW icons.
add_action( $after_hook, function() {
social_warfare();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment