SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if post has gallery (WordPress)

    Is there any way to check if a post has a gallery? I'm using a plugin that adds a jquery thickbox to galleries.. but it loads jQuery and the thickbox scripts on every page even though posts with galleries are the only pages that will use those javascripts. I don't want my users to download something they might not need during their visit

    I was thinking of doing something like:

    Code PHP:
    if (do_shortcode('[gallery]')) {
     //load javascripts
    }

    or

    Code PHP:
    $checkgallery = do_shortcode('[gallery]');
    if (!empty($checkgallery) {
     //load javascripts
    }

  2. #2
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try something like this
    PHP Code:
    if (gallery_shortcode($post->ID)){
      
    //load javascript


  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, it works in the templates, but not the plugin file:

    Code PHP:
    function add_lightbox_gallery_jquery() {
    global $post;
    	if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) {
    		wp_enqueue_script( 'jquery');
    		wp_enqueue_script('dimensions', '/wp-content/plugins/lightbox-gallery/js/jquery.dimensions.js', array('jquery'));
    		wp_enqueue_script('bgtiframe', '/wp-content/plugins/lightbox-gallery/js/jquery.bgiframe.js', array('jquery'));
    		wp_enqueue_script('lightbox', '/wp-content/plugins/lightbox-gallery/js/jquery.lightbox.js', array('jquery'));
    		wp_enqueue_script('tooltip', '/wp-content/plugins/lightbox-gallery/js/jquery.tooltip.js', array('jquery'));
    		wp_enqueue_script('lightbox-gallery', '/wp-content/plugins/lightbox-gallery/lightbox-gallery.js', array('jquery'));
    	}
    }

  4. #4
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it looks like an issue with your calls to wp_enqueue_script. since this works

    PHP Code:
    function add_lightbox_gallery_jquery() {
    global 
    $post;
        if (
    gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) {
      echo 
    '<!-- true -->';
        }


  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaronjj View Post
    it looks like an issue with your calls to wp_enqueue_script. since this works

    PHP Code:
    function add_lightbox_gallery_jquery() {
    global 
    $post;
        if (
    gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) {
      echo 
    '<!-- true -->';
        }

    Well if it was false it wouldn't even run anything within the if statement, right? I can't figure it out.. I'll try messing with it more tonight

  6. #6
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DanSEO View Post
    Well if it was false it wouldn't even run anything within the if statement, right? I can't figure it out.. I'll try messing with it more tonight
    right, the statement evaluates to true. That's why the code I posted echos 'true' on a post with a gallery.

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaronjj View Post
    right, the statement evaluates to true. That's why the code I posted echos 'true' on a post with a gallery.
    If it isn't true it shouldn't even run the wp_enqueue_script functions, but those scripts are still showing up on pages without a gallery. When I replace it with your snippet of code it displays <!-- TRUE --> on all pages, regardless if they have a gallery or not. Could it be that gallery_shortcode($post->ID) is returning something?

    Using WP 2.6.0

    Code PHP:
    function add_lightbox_gallery_jquery() {
     
    global $post;
     
        if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) {
     
    		wp_enqueue_script( 'jquery');
    		wp_enqueue_script('dimensions', '/wp-content/plugins/lightbox-gallery/js/jquery.dimensions.js', array('jquery'));
    		wp_enqueue_script('bgtiframe', '/wp-content/plugins/lightbox-gallery/js/jquery.bgiframe.js', array('jquery'));
    		wp_enqueue_script('lightbox', '/wp-content/plugins/lightbox-gallery/js/jquery.lightbox.js', array('jquery'));
    		wp_enqueue_script('tooltip', '/wp-content/plugins/lightbox-gallery/js/jquery.tooltip.js', array('jquery'));
    		wp_enqueue_script('lightbox-gallery', '/wp-content/plugins/lightbox-gallery/lightbox-gallery.js', array('jquery'));
     
        }
     
    }

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I did:
    Code PHP:
    function add_lightbox_gallery_jquery() {
     
    global $post;
     
        if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) {
     
        echo '<!--' . gallery_shortcode($post->ID) . '-->';
     
        }
     
    }

    And it returned:
    Code HTML4Strict:
    <!--
    		<style type='text/css'>
    			.gallery {
    				margin: auto;
    			}
    			.gallery-item {
    				float: left;
    				margin-top: 10px;
    				text-align: center;
    				width: 33%;			}
    			.gallery img {
    				border: 2px solid #cfcfcf;
    			}
    			.gallery-caption {
    				margin-left: 0;
    			}
    		</style>
    		<!-- see gallery_shortcode() in wp-includes/media.php -->
    		<div class='gallery'><dl class='gallery-item'>
    			<dt class='gallery-icon'>
    				<a href='http://gameviper.com/ps3/resistance-2-release-date-collectors-edition-global-multiplayer-and-co-op-beta-confirmed/attachment/resistancethumb' title='resistancethumb'><img src="http://gameviper.com/wp-content/uploads/2008/09/resistancethumb.jpg" width="77" height="100" class="attachment-thumbnail" alt="" /></a>
    			</dt></dl>
    			<br style='clear: both;' />
    		</div>
    -->

    gallery_shortcode($post->ID) will return something if there are images attached to the post, but regardless of whether the [gallery] shortcode was used in the post. Since I use images in pretty much every post, it will always return false

  9. #9
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DanSEO View Post
    gallery_shortcode($post->ID) will return something if there are images attached to the post, but regardless of whether the [gallery] shortcode was used in the post.
    Oops, that won't work.

    I just tried that first piece of code you pasted, if (do_shortcode('[gallery]')), and it worked for me. What was the problem with using that?

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaronjj View Post
    Oops, that won't work.

    I just tried that first piece of code you pasted, if (do_shortcode('[gallery]')), and it worked for me. What was the problem with using that?
    It gave me some php error the last time I tried it, I'll try again tonight though. But I think it will give the same result as gallery_shortcode($post->ID) since I add at least one image to every post.

    One thing I could do is make a function that goes through the post contents and searches for "[gallery]".. an awful lot to do just to get rid of those scripts

    It usually wouldn't bug me that much, but it loads 8 javascripts just for this one feature that I seldom use. If it was all in one file it wouldn't bug me, but it just adds another possible issue by making the browser load 8 more files (and it can only load 2 scripts in parallel)

    EDIT: Yeah, do_shortcode('[gallery]') does pretty much the same thing as gallery_shortcode($post->ID)

  11. #11
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm... I would think there would be an easier way to do this. $array = shortcode_parse_atts($post->post_content); will give you an array you can itterate through to check for [gallery]

    Best of luck.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •