Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Last active August 29, 2015 14:25
Show Gist options
  • Save bdeleasa/1fd46c8b9552c91356a6 to your computer and use it in GitHub Desktop.
Save bdeleasa/1fd46c8b9552c91356a6 to your computer and use it in GitHub Desktop.
Removes all default scripts and styles outputted by the Nextgen Gallery plugin. It also outputs the necessary code to replace the lightbox with the Foundation 5 reveal modal.
<?php
new Nextgen_Gallery_Customizations();
/**
* Class Nextgen_Gallery_Customizations
*
* The class responsible for removing all of the default scripts and
* styles that Nextgen outputs and replacing the scripts with the
* necessary jQuery to activate the Foundation 5 Reveal modal. This
* class doesn't handle outputting the actual Foundation 5 Reveal modal
* script.
*/
class Nextgen_Gallery_Customizations {
/**
* Registers all of our main actions
*
* @param none
* @return none
*
* @since 1.0.0
*/
function __construct() {
add_action( 'init', array($this, 'hide_nextgen_scripts') );
add_action( 'wp_footer', array($this, 'output_ngg_reveal_modal') );
add_action( 'wp_print_footer_scripts', array($this, 'output_reveal_script') );
}
/**
* Hides the Nextgen scripts and styles. We have all of the
* styles and scripts covered in our theme, so there's
* no need to output Nextgen's styles.
*
* @param none
* @return none
*
* @since 1.0.0
*/
function hide_nextgen_scripts() {
if ( !is_admin() ) {
define('NGG_SKIP_LOAD_SCRIPTS', true);
add_action( 'wp_print_styles', function() { wp_deregister_style('NextGEN'); }, 100);
}
}
/**
* Check if the given post uses a shortcode.
*
* @param string $shortcode
* @return bool
*
* @since 1.0.0
*/
function post_has_shortcode( $shortcode = '', $id = NULL ) {
global $post;
$post_id = !empty($id) ? $id : $post->ID;
$post_to_check = get_post($post_id);
// false because we have to search through the post content first
$found = false;
// if no short code was provided, return false
if (!$shortcode) {
return $found;
}
// check the post content for the short code
if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false ) {
// we have found the short code
$found = true;
}
// return our final results
return $found;
}
/**
* Check if the given post has a NextGen Gallery shortcode
*
* @param none
* @return none
*
* @since 1.0.0
*/
function has_nextgen_shortcode() {
global $post;
$nextgen_shortcodes = array(
'nggalbum',
'album',
'ngg-images',
'random',
'recent',
'thumb',
'slideshow',
'nggallery',
'nggtags',
'nggslideshow',
'nggrandom',
'nggrecent',
'nggthumb',
'imagebrowser',
'nggimagebrowser',
'singlepic',
'nggsinglepic',
'tagcloud',
'nggtagcloud',
);
$has_nextgen_shortcode = false;
// Check whether the page has one of the shortcodes
foreach($nextgen_shortcodes as $shortcode)
{
if ( $this->post_has_shortcode($shortcode) )
{
$has_nextgen_shortcode = true;
break;
}
}
// Check if the post content has the image placeholder instead of
// a shortcode
$content = $post->post_content;
if ( stristr($content, 'ngg_displayed_gallery') ) {
$has_nextgen_shortcode = true;
}
return $has_nextgen_shortcode;
}
/**
* Outputs the code for a reveal modal if the current
* page has a gallery on it.
*
* @param none
* @return none
*/
function output_ngg_reveal_modal() {
if ( !is_admin() && $this->has_nextgen_shortcode() ) {
echo '<div id="ngg-modal" class="reveal-modal" data-reveal><div class="ngg-image"></div><a class="close-reveal-modal" aria-label="Close">&#215;</a></div>';
}
}
/**
* Outputs some custom jQuery that will activate the Foundation 5 Reveal
* modal when clicking on a Nextgen thumbnail image.
*
* @param none
* @return none
*
* @since 1.0.0
*/
function output_reveal_script() {
?>
<script type="text/javascript">
(function($) {
$('a.ngg-fancybox').on('click', function() {
var img = $(this).data('src');
var revealId = 'ngg-modal';
$('#' + revealId + ' .ngg-image').html('<img src="' + img + '" alt="" style="max-width: 100%; height: auto; display: block; margin: 0 auto;" />');
$('#' + revealId).foundation('reveal','open');
return false;
});
})(jQuery);
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment