WordPress.org

Ready to get started?Download WordPress

Forums

Overriding post_gallery causes me to lose attributes (2 posts)

  1. pers0n
    Member
    Posted 1 year ago #

    I'm overriding post_gallery in functions.php but its losing the values of the attributes. Any idea why this is happening?

    function custom_gallery_shortcode_override($attr)
    {
    	global $post;
    	$output = '';
    	$lgal = '';
    	$cols = 0;
    	$ul_closed = false;
    	$size_selected = 'tiny';
    
    	//Extract default gallery settings
    	extract(shortcode_atts(array(
    		'order'      => 'ASC',
    		'orderby'    => 'menu_order ID',
    		'id'         => $post->ID,
    		'itemtag'    => 'dl',
    		'icontag'    => 'dt',
    		'captiontag' => 'dd',
    		'columns'    => 3,
    		'size'       => $size_selected,
    		'include'    => '',
    		'exclude'    => ''
    	), $attr));
    
    	$attachments = get_children(array(
    		'post_parent' => $id,
    		'post_status' => 'inherit',
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image',
    		'order' => $order,
    		'orderby' => $orderby
    	));
    
        if (!empty($attachments))
    	{
    		$output .= '<div id="gal-' . $id . '" class="gal">' . "\n";
    		$output .= '<ul class="gal-thumbs">';
    
    		//Loop through each attachment
    		foreach ($attachments as $image)
    		{
    			if ($ul_closed)
    			{
    				$output .= '<ul class="gal-thumbs">';
    				$ul_closed = false;
    			}
    
    			$att_img = wp_get_attachment_image($image->ID, $size_selected);//img tag
    			$att_url = wp_get_attachment_url($image->ID);//url to image
    			$att_link = get_attachment_link($image->ID);//url to "attachment page" - page with image on it
    			$postlink = get_permalink($image->post_parent);//url to post that has attachments
    			$att_desc = trim(htmlentities($image->post_content, ENT_QUOTES, 'UTF-8', true));//description
    			$att_cap = trim(htmlentities($image->post_excerpt, ENT_QUOTES, 'UTF-8', true));//caption
    			$att_title = trim(htmlentities($image->post_title, ENT_QUOTES, 'UTF-8', true));
    
    			$big_array = image_downsize($image->ID, 'large');
     			$large_link = $big_array[0];//large
    
    			$output .= '<li class="gal-item">';
    
    			echo 'link=' . $attr['link'];//DEBUG - THIS SHOWS NOTHING!!!!
    
    			//see if gallery was set to file or not
    			if (isset($attr['link']) && 'file' == $attr['link'])
    			{
    				//wp_get_attachment_link($id, $size, false, false);//get file
    				$l = $att_url;
    			}
    			else
    			{
    				//wp_get_attachment_link($id, $size, true, false);//get permalink to attachment page
    				$l = $att_link;
    			}
    
    			$output .= '<a class="gal-thumb" href="' .  $l . '" title="' . $att_title . '" data-url="' . $large_link . '">';
    
    			$output .= $att_img;
    
    			$output .= ' <span>';
    			$output .= $att_title;
    
    			if ($att_cap != '')
    			{
    				$output .= ' - ' . $att_cap;
    			}
    
    			$output .= '</span>';
    
    			$output .= '</a>';
    			$output .= '</li>' . "\n";
    
    			if ($columns > 0 && ++$cols % $columns == 0)
    			{
    				$output .= '</ul>';
    				$ul_closed = true;
    			}
    		}
    
    		if (!$ul_closed)
    		{
    			$output .= '</ul>';
    		}
    
    		$output .= '</div><!--.gal-->' . "\n";
    	}
    
    	return $lgal . $output;
    }
    
    add_filter('post_gallery', 'custom_gallery_shortcode_override', 10, 2);
  2. Lumpysimon
    Member
    Posted 1 year ago #

    Bit of a late reply this, but posting it here in case it's useful to anyone.

    The $attr parameter is the second parameter in the post_gallery filter . The first parameter is the string to be returned, which is always passed in as an empty string.

    So your function declaration needs to be:

    function custom_gallery_shortcode_override( $empty, $attr )

Topic Closed

This topic has been closed to new replies.

About this Topic