<?php
function andywar_gallery_shortcode($empty , $attr) {
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 2,
'size' => 'medium',
'include' => '',
'exclude' => '',
'link' => ''
), $attr, 'gallery'));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$icontag = tag_escape($icontag);
$valid_tags = wp_kses_allowed_html( 'post' );
if ( ! isset( $valid_tags[ $itemtag ] ) )
$itemtag = 'dl';
if ( ! isset( $valid_tags[ $captiontag ] ) )
$captiontag = 'dd';
if ( ! isset( $valid_tags[ $icontag ] ) )
$icontag = 'dt';
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
/* see gallery_shortcode() in wp-includes/media.php */
</style>";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
if ( ! empty( $link ) && 'file' === $link )
$image_output = wp_get_attachment_link( $id, $size, false, false );
elseif ( ! empty( $link ) && 'none' === $link )
$image_output = wp_get_attachment_image( $id, $size, false );
else
$image_output = wp_get_attachment_link( $id, $size, true, false );
$image_meta = wp_get_attachment_metadata( $id );
$orientation = '';
if ( isset( $image_meta['height'], $image_meta['width'] ) )
$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon {$orientation}'>
$image_output
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
add_filter('post_gallery' , 'andywar_gallery_shortcode' , 10 , 2);
?>
]]><style type='text/css'>
#gallery-1 {
margin: auto;
}
#gallery-1 .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: 33%;
}
#gallery-1 img {
border: 2px solid #cfcfcf;
}
#gallery-1 .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->
I looked into the gallery_shortcode in wp_includes/media.php and found this code:
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->";
I feel that instead of adding border to the #selector the borders should be added to .gallery-icon. By choosing to add it to #selector which will be gallery-1 or gallery-2, we make it difficult for theme authors to change the style of gallery output.
I was wondering if someone can explain to me why WordPress is using #selector to add borders to images in gallery and not a class like gallery-icon or gallery-item.
]]>Here’s the method I’m currently using by editing wp-includes/media.php:
function gallery_shortcode($attr) {
global $post, $wp_locale;
static $instance = 0;
$instance++;
//KRG: Added for Arkmagia Gallery Shortcode.
// Allow plugins/themes to override the default gallery attributes.
$attr = apply_filters('post_gallery_attr', '', $attr);
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
...
I then have a plugin to set the default params:
add_filter('post_gallery_attr', 'arkmagia_gallery_shortcode');
function arkmagia_gallery_shortcode($attr) {
if ( !isset( $attr['link'] ) ) {
$attr['link'] = 'file';
}
return $attr;
}
Though there is also another problem in that if I wanted any instances to link to actual attachment pages, there is no explicit ‘link’ attribute value for that…
Any thoughts? Thanks for reading!
]]>How can customize the native wordpress and have 2 styles ([gallery1] [gallery2] shortcodes) instead of one?
I’ve tried (in media.php) to copy the default gallery shortcode ( add_shortcode('gallery', 'gallery_shortcode'); .......... return $output;}
), inserted it after the default one and changed everywhere in the code ‘gallery’ onto ‘gallery1’ and ‘gallery2’ respectively:
(add_shortcode('gallery1', 'gallery1_shortcode'); ...
and add_shortcode('gallery2', 'gallery2_shortcode'); ...)
I’ve tried and inserted [gallery1] and [gallery2] into a post one after another but it didn’t work well – the images were framed and resized as writted in style.css, but it seemed that both galleries were inserted twice and one overlayed the other one.
I’m not an advanced wp-user, nor a designer or coder. I understand some parts of the code and know how to style the css parts, but media.php is a complete mess for me Could anyone, please, help me with this code, cause I’m definitely doing something wrong
Thanks in advance! Greya
]]>After searching the files I found the gallery_shortcode is being generated in:
wp-includes/media.php
But after changing some code in that file, the changes are not visible in the webpage :/
I can also delete the whole function gallery_shortcode($attr) and it still shows the gallery normally.
It looks like the code is coming from somewhere else. Can anyone check this for me. It really sucks!
]]><a>
with rel attribute.
Suggested solution (additions indicated with !!!):
Add rel attribute to the gallery shortcode and inject this attribute to the reference tag.
function gallery_shortcode($attr) {
global $post;
static $instance = 0;
$instance++;
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'rel' => '', // !!! Add attribute
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail'
), $attr));
$id = intval($id);
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$selector = "gallery-{$instance}";
$output = apply_filters('gallery_style', "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: {$itemwidth}%; }
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->
<div id='$selector' class='gallery galleryid-{$id}'>");
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
if( ! empty($rel) ) { !!! // Add rel injection
$link = str_replace('<a href=', "<a rel='$rel' href=", $link);
}
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
]]>function gallery_shortcode($attr) {
global $post;
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
), $attr));
$id = intval($id);
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $id => $attachment )
$output .= wp_get_attachment_link($id, $size, true) . "\n";
return $output;
}
$listtag = tag_escape($listtag);
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$output = apply_filters('gallery_style', "
<style type='text/css'>
.gallery {
margin: auto;
}
.gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: {$itemwidth}%; }
.gallery img {
border: 2px solid #cfcfcf;
}
.gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->
<div class='gallery'>");
foreach ( $attachments as $id => $attachment ) {
$link = wp_get_attachment_link($id, $size, true);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='gallery-caption'>
{$attachment->post_excerpt}
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
What I want to do is override this function and use an ul with no style tag in the middle of my body. Searching around, I found some useful pieces of code and managed to write this:
function remove_gallery_css()
{
return "<ul class=\"gallery\">";
}
add_filter('gallery_style', 'remove_gallery_css');
function fix_gallery_output( $output )
{
$output = preg_replace("%</div>%", "</ul>", $output);
return $output;
}
add_filter('the_content', 'fix_gallery_output',11, 1);
The first functions removes the <style> tag and works like a charm. It also removes the <div class=”gallery”> and replaces it with an ul. The problem comes with the second function. It basically removes any </div> from the_content and replaces it with an /ul
. Works perfectly unless you have a </div> (div end tag) somewhere else in your posts/pages.
Si the question is, how do I replace the gallery_shortcode output of </div> with an /ul without affecting anything else? Of course, I could hack media.php, but I’d like to distribute this as a plugin so people won’t have to hack media.php
]]>However I’d like to be able to distribute this fix inside a theme without having to punt a hacked media.php. Is there any way to replace the gallery_shortcode function using functions.php?
In short, is the gallery_shortcode function pluggable in a theme?
]]>