Cinder
Cinder

Reputation: 349

WordPress: How can I get all comments attachments?

By running a query like the following, I can get all post attachments and display them on a page:

$attachments = get_posts( array(
    'post_type'   => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID
) );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    ?>
      <li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
        <p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
      </li>
    <?php
  }
}

Now if I want to get all comments attachments instead, how would I go about that?

This is my comments query, so far:

    $args = array(
        'status' => 'approve',
        'type' => 'review',
        'number' => $reviews_per_page,
        'offset' => $offset
    );
    
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );
    
    // Comment Loop
    if ( $comments ) {
        echo "<ol>";
        foreach ( $comments as $comment ): ?>
           // stuff to display the comments

           // display image
           <div class="comment-images">
             <li><?php echo wp_get_attachment_image( $comment->comment_ID, 'full' ); ?></li>
           </div>

     <?php endforeach;
      echo "</ol>"; ?>

Please not that stuff to display comments works as a placeholder in this code snippet to keep it short. If required, I will of course post the full code. What the placeholder code does is, it pulls in all comment texts and displays them in one page. The part I struggle with, is pulling in the image(s) attached to each comment along with the text.

It is my goal to display all comments in one page, with their according image attachments (since users are permitted to upload an image file with their comment). But I am not sure how to get the attached images, since wp_get_attachment_image seems to work on posts only. Any ideas how I can accomplish this?

EDIT:

How an attachment is added to WP comments:

To add attachments to a comment, I'm using the free plugin DCO Comment Attachment. I've looked through the code. Below, I will provide the parts I think are relevant for my attempt.

Inside the DCO_CA_Base class, this function gets an assigned attachment ID:

public function get_attachment_id( $comment_id = 0 ) {
    $meta_key = $this->get_attachment_meta_key();

    if ( ! $comment_id ) {
        $comment_id = get_comment_ID();
    }

    return get_comment_meta( $comment_id, $meta_key, true );
}

This checks if a comment has an attachment:

public function has_attachment( $comment_id = 0 ) {
    if ( ! $comment_id ) {
        $comment_id = get_comment_ID();
    }

    $attachment_id = $this->get_attachment_id( $comment_id );
    if ( ! $attachment_id ) {
        return false;
    }

    // Check the attachment exists.
    if ( ! wp_get_attachment_url( $attachment_id ) ) {
        return false;
    }

    return true;
}

And this assigns an attachment for a comment:

public function assign_attachment( $comment_id, $attachment_id ) {
    $meta_key = $this->get_attachment_meta_key();
    return update_comment_meta( $comment_id, $meta_key, $attachment_id );
}

Lastly, here, the HTML markup is generated, which displays the comment with the images attached to it. The use of DCO Comment Attachment in this code actually gave me the idea for my own attempt:

public function get_attachment_preview( $attachment_id ) {
        $url = wp_get_attachment_url( $attachment_id );

        if ( ! $url ) {
            return false;
        }

        $embed_type = $this->get_embed_type( $attachment_id );

        switch ( $embed_type ) {
            case 'image':
                $thumbnail_size = $this->get_option( 'thumbnail_size' );
                if ( is_admin() ) {
                    $thumbnail_size = 'medium';
                }

                $img = DCO Comment Attachment( $attachment_id, $thumbnail_size );
                if ( ! is_admin() && $this->get_option( 'link_thumbnail' ) ) {
                    $full_img_url = wp_get_attachment_image_url( $attachment_id, 'full' );
                    $img          = '<a href="' . esc_url( $full_img_url ) . '">' . $img . '</a>';
                }

                $attachment_content = '<p class="dco-attachment dco-image-attachment">' . $img . '</p>';
                break;
            case 'video':
                $attachment_content = '<div class="dco-attachment dco-video-attachment">' . do_shortcode( '[video src="' . esc_url( $url ) . '"]' ) . '</div>';
                break;
            case 'audio':
                $attachment_content = '<div class="dco-attachment dco-audio-attachment">' . do_shortcode( '[audio src="' . esc_url( $url ) . '"]' ) . '</div>';
                break;
            case 'misc':
                $title              = get_the_title( $attachment_id );
                $attachment_content = '<p class="dco-attachment dco-misc-attachment"><a href="' . esc_url( $url ) . '">' . esc_html( $title ) . '</a></p>';
        }

        return $attachment_content;
    }

Upvotes: 1

Views: 792

Answers (1)

Cinder
Cinder

Reputation: 349

I think I got it! :) I'm putting it here in case it helps others.

For completion's sake: this solution works with the awesome plugin DCO Comment Attachment that allows users to add attachments (like images or videos) to their comments.

With the code posted above, I display all comments in one page. By adding the following piece of code, the images added via the plugin show up with their according comments.

I use get_comment_meta(); to fetch the meta_value named attachment ID from the database. The field meta_value was added by the plugin to the database wp_commentmeta. Then I pass the attachment ID on to wp_get_attachment_image_url(), which gets the url of the image attached to the attachment ID.

This is my code now:

<div class="review-images">
  <?php
  //get meta_key 'attachment_id' and according meta_value from database wp_commentmeta
  //where comment_id == $comment->comment_ID;
    
   $attachment_id = get_comment_meta( $comment->comment_ID, 'attachment_id', true );
   $image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
    
   // output image to screen
   echo '<img loading="lazy" src="' . $image_url . '" alt="attachment image" /> ';
   ?>
</div>

Upvotes: 1

Related Questions