Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

converting current attachment code to use Attachment plugin WordPress

  • SOLVED

[[LINK href="http://"]][[/LINK]]Like it says on the tin, I have a simple function which will gather all attached images post and, and stuff it into a gallery - its indicative of how I've handled a bunch of other attachement work as well:
(criticism of the code is always welcome)

<strong>My Code</strong>
function tr_get_attachments_image($post){
global $post;
$_content_attach_images = '';

$images = get_children( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order ID',
'post_mime_type' => 'image',
) );
if ( $images ){
$_content_attach_images = '[fancybox]<div class="attached_gallery">';
$_content_attach_images .= '<h4 class="gallery">Image Gallery</h4>';
$_content_attach_images .= do_shortcode('[gallery id="' . intval( $post->ID ) . '"]');
$_content_attach_images .= '</div>';
}
echo apply_filters('the_content', $_content_attach_images );
}


While this works just fine, in general the handeling of attachments is awkward for the user (me!).
So I'd like to give this Attachments plugin a try. Its just that the author's example is to far away from what I know. Would someone mind having a bash at writing up an equivalent from what I've got to leverage the plugin?

from the [[LINK href="http://wordpress.org/extend/plugins/attachments/other_notes/"]] Attachments Plugin[[/LINK]] Support page:
http://wordpress.org/extend/plugins/attachments/other_notes/

<blockquote>Firing attachments_get_attachments() returns an array consisting of all available Attachments. Currently each Attachment has four pieces of data available:

title - The attachment Title
caption - The attachment Caption
id - The WordPress assigned attachment id (for use with other WordPress media functions)
location - The attachment URI
mime - The attachment MIME type (as defined by WordPress)
filesize - Formatted file size</blockquote>

<strong>Their Example:</strong>

<?php
if( function_exists( 'attachments_get_attachments' ) )
{
$attachments = attachments_get_attachments();
$total_attachments = count( $attachments );
if( $total_attachments ) : ?>
<ul>
<?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
<li><?php echo $attachments[$i]['title']; ?></li>
<li><?php echo $attachments[$i]['caption']; ?></li>
<li><?php echo $attachments[$i]['id']; ?></li>
<li><?php echo $attachments[$i]['location']; ?></li>
<li><?php echo $attachments[$i]['mime']; ?></li>
<li><?php echo $attachments[$i]['filesize']; ?></li>
<?php endfor; ?>
</ul>
<?php endif; ?>
<?php } ?>

Answers (2)

2012-10-28

Christianto answers:

Hi,

Your code to get all attachement:

$images = get_children( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order ID',
'post_mime_type' => 'image',
) );

only use to check if attachment is exists:

if ( $images ){
$_content_attach_images = '[fancybox]<div class="attached_gallery">';
$_content_attach_images .= '<h4 class="gallery">Image Gallery</h4>';
$_content_attach_images .= do_shortcode('[gallery id="' . intval( $post->ID ) . '"]');
$_content_attach_images .= '</div>';
}

the images itself are using [gallery] shortcode (which using get_children() to populate images attachment).

If you want to use attachment images which comes from plugin, you can edit gallery shortcode on media.php
Or creating new shortcode which will use plugin function attachments_get_attachments();

this is for example the new shortcode gallery code (untested),
you can check the difference between the original on media.php line 777:

add_shortcode('gallery_attach', 'attachment_gallery_shortcode');

/**
* The Gallery shortcode.
*/
function attachment_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',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));

$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';

if( function_exists( 'attachments_get_attachments' ) ){
$_attachments = attachments_get_attachments();

$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} else {
return '';
}

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;
$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;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->";
$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 ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->caption) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->caption) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}

$output .= "
<br style='clear: both;' />
</div>\n";

return $output;
}


and this your function, using new [gallery_attach] shortcode:
function tr_get_attachments_image($post){

global $post;
$_content_attach_images = '';

if( function_exists( 'attachments_get_attachments' ) ){

$attachments = attachments_get_attachments();
$images = count( $attachments );

if ( $images ){
$_content_attach_images = '[fancybox]<div class="attached_gallery">';
$_content_attach_images .= '<h4 class="gallery">Image Gallery</h4>';
$_content_attach_images .= do_shortcode('[gallery_attach id="' . intval( $post->ID ) . '"]');
$_content_attach_images .= '</div>';
}

echo apply_filters('the_content', $_content_attach_images );
}
}


SuperAgent comments:

Humm, I didn't realise this about the gallery shortcode. . . . that muddies the water a bit.

I think I've backfired on this question. My goal is <em>not</em> to rewrite the gallery shortcode, but instead to understand how to work with values out of 'attachments_get_attachments()'. I was just using the example the gallery shortcode was just one I had close to hand.

I think I should have asked instead to output a list of attachments filtered by mime type, using my code as a basis.

Am I allowed to revise this question?


Christianto comments:

Did you mean like this?

function tr_get_attachments_image($post){

global $post;
$_content_attach_images = '';

if( function_exists( 'attachments_get_attachments' ) ){

$attachments = attachments_get_attachments();
$attachments_count = count( $attachments );

if ( $attachments_count ){

$_content_attach_images = '[fancybox]<div class="attached_gallery">';
$_content_attach_images .= '<h4 class="gallery">Image Gallery</h4>';

for( $i=0; $i<$attachments_count; $i++ ){

// if attachment is jpeg, png, gif images extension
if($attachments[$i]['mime'] == 'image/jpeg' || $attachments[$i]['mime'] == 'image/png' || $attachments[$i]['mime'] == 'image/gif' ){

// get attachment data for full version and thumbnail version.
$full_image = wp_get_attachment_image_src($attachments[$i]['id'], 'full');
$thumb_image = wp_get_attachment_image_src($attachments[$i]['id'], 'thumbnail');

$_content_attach_images .= '<a class="fancybox images" href="'.$full_image[0].'" >';
$_content_attach_images .= '<img title="'.$attachments[$i]['title'].' "src="'.$thumb_image[0].'" width="'.$thumb_image[1].'" height="'.$thumb_image[2].'"/>';
$_content_attach_images .= '</a>';

// if attachment other like csv, txt, xml
} else if($attachments[$i]['mime'] == 'text/csv' || $attachments[$i]['mime'] == 'text/plain' || $attachments[$i]['mime'] == 'text/xml' ){

$attachment_link = $attachments[$i]['location'];
// or using wp_get_attachment url
// $attachment_link = wp_get_attachment_url($attachments[$i]['id']);

$_content_attach_images .= '<a class="fancybox file-attachment" href="'.$attachment_link.'" >';
$_content_attach_images .= 'this is '.$attachments[$i]['title'].' link';
$_content_attach_images .= '</a>';

}

}

$_content_attach_images .= '</div>';

}

echo apply_filters('the_content', $_content_attach_images );
}
}


SuperAgent comments:

bingo, this is excellent, what a relief. I can work with this quite easily.
Thank you!

2012-10-28

Arnav Joy answers:

can you explain little bit more about the thing you want ?


SuperAgent comments:

Based on my example (postsed top) , I want a explanation of how to use the plugin method attachments_get_attachments();method instead of WP's get_children().

Im not seeing the forest for the trees at the moment.


SuperAgent comments:

realised that the plugin link wasn't showing - just added it again. -OOPS!


Arnav Joy comments:

in place of attachments_get_attachments();
try this

global $post;

attachments_get_attachments($post);


SuperAgent comments:

we're trying to convert (my) first example . . . not the demonstration code given by the plugin author (the second example).


Arnav Joy comments:

try this


function tr_get_attachments_image($post){

global $post;

$_content_attach_images = '';




$attachments = attachments_get_attachments();

$total_attachments = count( $attachments );

if ( $total_attachments ){

$_content_attach_images = '[fancybox]<div class="attached_gallery">';

$_content_attach_images .= '<h4 class="gallery">Image Gallery</h4>';

$_content_attach_images .= do_shortcode('[gallery id="' . intval( $post->ID ) . '"]');

$_content_attach_images .= '</div>';

}

echo apply_filters('the_content', $_content_attach_images );

}


SuperAgent comments:

So far this is only outputs the div, and the heading - the gallery isn't generating.
I've note that, this dosen't testing for mime type, or pulling any of the other values -

I was expecting to need to filter this a bit, something like:
$attachments = attachments_get_attachments(array(
// somehow filter out just images from attachments
));


Arnav Joy comments:

if you want to revise your question then please do it , as it is not clear what you are looking for?