WordPress.org

Ready to get started?Download WordPress

Forums

NextGEN Gallery
Suggestion: wp_get_attachment_url() filter for "ngg-#" image IDs (4 posts)

  1. RoamFree
    Member
    Posted 2 years ago #

    I encountered another theme that didn't like to show NGG featured images, and digging into the code I found that it is because wp_get_attachment_url() does not recognize NGG's image IDs (ie, "ngg-1").

    There is a WP filter hook for wp_get_attachment_url(), and it seems trivial to detect when the url is "" (ie, the URL was not found) and the ID is "ngg-" followed by a number. I haven't found the NGG function for looking up the image URL, but surely one exists.

    Such a filter would probably fix a number of problems in themes. Could that be considered for a future version of NGG?

    http://codex.wordpress.org/Function_Reference/wp_get_attachment_url
    WP filter list: http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Reads

    http://wordpress.org/extend/plugins/nextgen-gallery/

  2. RoamFree
    Member
    Posted 2 years ago #

    Sigh. Won't work, if I'm reading it right. wp_get_attachment_url() tries to do a get_post() on the ID, and when that fails it returns false... without running the filters on the function. So code which does the proper lookup for NGG can't be inserted.

  3. RoamFree
    Member
    Posted 2 years ago #

    Themes which need to handle NGG images might be able to be patched with a child theme which uses something similar to this:

    /*
    	 * Function which returns the URL for a NextGen Gallery (NGG) image.  It was inspired by a theme which uses a line similar to:
    	 * 	$image_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
    	 * The above can be fixed to work with NGG by adding the following line after it:
    	 * 	if( function_exists( 'ngg_a_theme_child_theme_thumbnail_id_url' ) ) $image_url = ngg_a_theme_child_theme_thumbnail_id_url( $image_url, get_post_thumbnail_id( $post->ID ) );
    	 *
    	 * The first parameter, $image_url, must be FALSE or empty() in order for the function to look up the NGG image ID.  If the first parameter has anything in it, that value is returned.
    	 *
    	 * This function is needed because wp_get_attachment_url() does not invoke its filters when it does not recognize the NGG image IDs (ie, "ngg-1"), thus NGG cannot insert similar code.
    	 */
    	function ngg_a_theme_child_theme_thumbnail_id_url( $url='', $attachment_id='' ) {
    		$return_url = $url;	// Default value is whatever was passed in for the URL.
    		if( $url === false || empty($url) ) {
    			if( is_string($attachment_id) ) {
    				$attachment_id = trim($attachment_id);
    				if( substr($attachment_id, 0, 4) == 'ngg_' ) {
    					$numeric_id = substr($attachment_id, 4);	// Get the number after 'ngg-'
    					if( is_numeric($numeric_id) ) {
    						if( class_exists('nggdb') ) {
    							$ngg_image = nggdb::find_image($numeric_id);	// Find the image info in NGG's database
    							if( $ngg_image ) {
    								$ngg_url = $ngg_image->thumbURL;
    								if( ! empty($ngg_url) ) {
    									$return_url = $ngg_url;	// found the needed URL
    								} // if NGG gave us a URL
    							} // if ngg_image not null
    						} // if NGG DB call exists
    					} // if ends with numeric
    				} // if ID starts with 'ngg-'
    			} // if is_string
    		} // if no URL defined
    
    		// Return the result
    		return $return_url;
    	}
  4. RoamFree
    Member
    Posted 2 years ago #

    Nope, I don't know how 'ngg-' got changed to 'ngg_'. Used to be 'ngg-', as it should be.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic