Ask your WordPress questions! Pay money and get answers fast! (more info)

update attachments plugin code to new version WordPress

  • SOLVED

Im trying to update the below code so as to take advantage of the new version of the attachments plugin. With the new changes, Im not sure how to filter for attachment types and mime types.

The below is just one example - I have other functions which output different markup for document attachments ( ie pdf and word docs), and another that outputs markup for media players (ie video and audio).

I think if someone was able to update this, I'd be able to use it as an example to rewrite my other functions on my own.

Here is the plugin documentation on wp and github:
[[LINK href="https://github.com/jchristopher/attachments#usage"]]github[[/LINK]]
[[LINK href="http://wordpress.org/extend/plugins/attachments/"]]wordpress.org[[/LINK]]

// ========================================
// ! Returns attached images as a gallery
// ========================================
function tr_get_attachments_image($post){
global $post;
$content_attach = '';
$is_test = "";
if( function_exists( 'attachments_get_attachments' ) ){
$attachments = attachments_get_attachments();
$attachments_count = count( $attachments );

//see if there are any images
if ( $attachments_count ){
for ($i=0; $i < $attachments_count; $i++) {
$mime = strtok($attachments[$i]['mime'], "/");

if ( strstr($mime, 'image') ) {
$is_test = true;
break;
}
}
// if so only ouput the images
if ( $is_test == true ) {
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title">';
$content_attach .= '<h4 class="gallery">Image Gallery</h4>';
$content_attach .= '</div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
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 .= '<li class="gallery-item brick" >';
$content_attach.= '<a class="fancybox thumbnail" rel="gallery" title="'.$attachments[$i]['title'].'" href="'.$full_image[0].'" >';
$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments[$i]['title'] . '" title="' . $attachments[$i]['title'] . '" src="'.$thumb_image[0].'" width="'.$thumb_image[1].'" height="'.$thumb_image[2].'"/>';
$content_attach .= '</a>';
$content_attach .= '</li>';
}
}
$content_attach .= '</ul>';
$content_attach .= '</div>';

}
echo apply_filters('the_content', $content_attach );
}
//if ($is_test != true ) echo "<p>no gallery?</p>";
}
}





The below code is my half baked update - It outputs all attachments not just the images:
function tr_get_attachments_image(){
$id = get_the_ID();
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );

if( $attachments->get() ){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title">';
$content_attach .= '<h4 class="gallery">Image Gallery</h4>';
$content_attach .= '</div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';

while ( $attachments->get() ){
$full_image = wp_get_attachment_image_src($attachments->id('full'));
$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));

$content_attach .= '<li class="gallery-item brick" >';
$content_attach .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach .= '</a>';
$content_attach .= '</li>';
}
}
$content_attach .= '</ul>';
$content_attach .= '</div>';

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

Answers (1)

2012-12-23

Dbranes answers:

Hi, have you tried to use:

$attachments->type();

in your IF sentence, since it is constructed by using:

get_post_mime_type

in the plugin source code, i.e.

$attachment_mime = explode( '/', get_post_mime_type( $attachment->id ) );
$attachment->type = isset( $attachment_mime[0] ) ? $attachment_mime[0] : null;
$attachment->subtype = isset( $attachment_mime[1] ) ? $attachment_mime[1] : null;


Dbranes comments:

you could then try

if($attachments->type() =="image"){

}


to check for images or

if($attachments->type() =="image"){
if($attachments->subtype()=="jpeg" || $attachments->subtype()=="gif")


}
}


if you need to check for jpeg/gif/...


SuperAgent comments:

I have tried various iterations of `$attachments->type() =="image")` without any luck. It didn't seem that it would return true. But perhaps you could fold this suggestion into my second (but half baked) example?


Dbranes comments:

you can try this inside your while loop:


echo "<pre>";
$mytype=get_post_mime_type($attachments->id('full'));
print_r($mytype);
echo "</pre>";


to check the filetype.

ps: You should also try to test with more than one image attachment per post.






Dbranes comments:

I think there might be problem with this:

if( $attachments->get() ){

}


it will forward the attachments pointer by +1.

So if you have only one attachment, then the while loop gives nothing.

So try to remove the first if you have to test.


SuperAgent comments:

Dbranes,
Im really not good with array/object handeling - kind of why I asked the question. But if I run this:

function tr_test_print_attachments(){
$attachments = new Attachments( 'attachments' );
if( $attachments->exist() ){
while( $attachment = $attachments->get()) {
echo ('<li><pre>');
$mytype=get_post_mime_type($attachments->id('full'));
print_r($mytype);
echo ('</pre></li>');
}
}
}


I get a list of the attachment mime types.
The post Im testing on has a variety of attachment types to filter against.


Dbranes comments:


ok, how does this work?

function tr_get_attachments_image(){
$id = get_the_ID();
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );
if( $attachments->exist() ){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title">';
$content_attach .= '<h4 class="gallery">Image Gallery</h4>';
$content_attach .= '</div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
while ( $attachments->get() ){
if($attachments->type()=="image"){
$full_image = wp_get_attachment_image_src($attachments->id('full'));
$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));
$content_attach .= '<li class="gallery-item brick" >';
$content_attach .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach .= '</a>';
$content_attach .= '</li>';
}
}
}
$content_attach .= '</ul>';
$content_attach .= '</div>';
echo apply_filters('the_content', $content_attach );
}


Dbranes comments:

and here is another version using get_post_mime_type() directly:

function tr_get_attachments_image(){
$id = get_the_ID();
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );
if( $attachments->exist() ){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title">';
$content_attach .= '<h4 class="gallery">Image Gallery</h4>';
$content_attach .= '</div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
while ( $attachments->get() ){
$mytype=get_post_mime_type($attachments->id('full'));
if($mytype=="image/jpeg" || $mytype=="image/gif"){
$full_image = wp_get_attachment_image_src($attachments->id('full'));
$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));
$content_attach .= '<li class="gallery-item brick" >';
$content_attach .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach .= '</a>';
$content_attach .= '</li>';
}
}
}
$content_attach .= '</ul>';
$content_attach .= '</div>';
echo apply_filters('the_content', $content_attach );
}


SuperAgent comments:

much much closer - with some tweaks below.

the problem is these two are resolving thumbnail url:
$full_image = wp_get_attachment_image_src($attachments->id('full'));
$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));


this is what I have so far - note the test to prevent gallery headline appearing if images are not returned:

function tr_get_attachments_image(){
$id = get_the_ID();
$is_test = false;
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );
if( $attachments->exist() ){
while ( $attachments->get() ){
if($attachments->type()=="image"){
$is_test = true;
break;
}
}
if($is_test == true){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title"><h4 class="gallery">Image Gallery</h4></div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
while ( $attachments->get() ){
$mytype=get_post_mime_type($attachments->id('full'));
if($mytype=="image/jpeg" || $mytype=="image/gif"){
$full_image = wp_get_attachment_image_src($attachments->id('full'));
$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));
$content_attach .= '<li class="gallery-item brick" >';
$content_attach .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach .= '</a>';
$content_attach .= '</li>';
}
}
}
$content_attach .= '</ul>';
$content_attach .= '</div>';
echo apply_filters('the_content', $content_attach );
}
}


SuperAgent comments:

a-ha, I think I see what your saying about the offset - the code above misses the first attached image, strange. What's that about?


Dbranes comments:


each time you call $attachments->get() the attachment array position is forwarded +1.

what about this:

function tr_get_attachments_image(){
$id = get_the_ID();
$bFoundImages = false;
$content_attach_li = '';
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );
if( $attachments->exist() ){
while ( $attachments->get() ){
if($attachments->type()=="image"){
$bFoundImages = true;
$full_image = wp_get_attachment_image_src($attachments->id(),'full');
$thumb_image = wp_get_attachment_image_src($attachments->id(),'thumbnail');
$content_attach_li .= '<li class="gallery-item brick" >';
$content_attach_li .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach_li .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach_li .= '</a>';
$content_attach_li .= '</li>';
}
}
}
if($bFoundImages){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title"><h4 class="gallery">Image Gallery</h4></div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
$content_attach .= $content_attach_li;
$content_attach .= '</ul>';
$content_attach .= '</div>';
echo apply_filters('the_content', $content_attach );
}
}


SuperAgent comments:

Separating it out bFoundImages works well,
Though Im not quite sure why, the image thumbnail and full urls are resolving as well.
Are we not able to use the if($mytype=="image/jpeg" || $mytype=="image/gif"){ at this point?

Its not a concern so much for images and documents, but when I get to audio and video it may become an issue.


Dbranes comments:

ok great,

in the laste code example, I made a small change to your code:

from:

$full_image = wp_get_attachment_image_src($attachments->id('full'));
$thumb_image = wp_get_attachment_image_src($attachments->id('thumbnail'));


to:

$full_image = wp_get_attachment_image_src($attachments->id(),'full');
$thumb_image = wp_get_attachment_image_src($attachments->id(),'thumbnail');


You should be able to use both:

a) with type/subtype:

function tr_get_attachments_image(){
$id = get_the_ID();
$bFoundImages = false;
$content_attach_li = '';
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );
if( $attachments->exist() ){
while ( $attachments->get() ){
if($attachments->type()=="image"){
if($attachments->subtype()=="jpeg" || $attachments->subtype()=="gif" || $attachments->subtype()=="png"){
$bFoundImages = true;
$full_image = wp_get_attachment_image_src($attachments->id(),'full');
$thumb_image = wp_get_attachment_image_src($attachments->id(),'thumbnail');
$content_attach_li .= '<li class="gallery-item brick" >';
$content_attach_li .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach_li .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach_li .= '</a>';
$content_attach_li .= '</li>';
}
}
}
}
if($bFoundImages){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title"><h4 class="gallery">Image Gallery</h4></div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
$content_attach .= $content_attach_li;
$content_attach .= '</ul>';
$content_attach .= '</div>';
echo apply_filters('the_content', $content_attach );
}
}


b) with get_post_mime_type() :

function tr_get_attachments_image(){
$id = get_the_ID();
$bFoundImages = false;
$content_attach_li = '';
$content_attach = '';
$attachments = new Attachments( 'attachments', $id );
if( $attachments->exist() ){
while ( $attachments->get() ){
$mytype=get_post_mime_type($attachments->id());
if($mytype=="image/jpeg" || $mytype=="image/gif"){
$bFoundImages = true;
$full_image = wp_get_attachment_image_src($attachments->id(),'full');
$thumb_image = wp_get_attachment_image_src($attachments->id(),'thumbnail');
$content_attach_li .= '<li class="gallery-item brick" >';
$content_attach_li .= '<a class="fancybox thumbnail" rel="gallery" title="'. $attachments->field( 'title' ) .'" href="'. $full_image[0] .'" >';
$content_attach_li .= '<img class="attachment-thumbnail" alt="' . $attachments->field( 'title' ) . '" title="' . $attachments->field( 'title' ) . '" src="'. $thumb_image[0] .'" width="'. $thumb_image[1] .'" height="'.$thumb_image[2].'"/>';
$content_attach_li .= '</a>';
$content_attach_li .= '</li>';
}
}
}
if($bFoundImages){
$content_attach = '[fancybox]<div class="attached_gallery">';
$content_attach .= '<div class="attachment-title"><h4 class="gallery">Image Gallery</h4></div>';
$content_attach .= '<ul id="" class="thumbnails gallery">';
$content_attach .= $content_attach_li;
$content_attach .= '</ul>';
$content_attach .= '</div>';
echo apply_filters('the_content', $content_attach );
}
}


so I think you can use both versions as a model for other mime types.


SuperAgent comments:

Dbranes,

Many thanks -- I'll have to check this tomorrow, but at first glance I don't have any questions. Thanks taking this up and walking me though it --- and seeing how its almost Christmas eve, happy Holidays too. Dont forget to get out from behind the screen!

Cheers,
Ben