WordPress wp_attachment_is_image() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns true or false

Definition

In WordPress, wp_attachment_is_image() checks if an attachment is an image. Files in the Media Library can be images, videos, PDFs, and more.

It returns true for an image and false for anything else, so you use it in an if statement.

Syntax

A few ways to call wp_attachment_is_image().

WordPress
<?php
if ( wp_attachment_is_image( $attachment_id ) ) {
  echo 'This is an image.';
}
CallWhat it does
wp_attachment_is_image()Check the current attachment.
wp_attachment_is_image( 15 )Check the attachment with ID 15.
wp_attachment_is_image( $post )Check an attachment post object.

It checks the file type of the attachment, not the page you are on.

Full declaration
WordPress
wp_attachment_is_image( int|WP_Post $post = null ): bool

Parameters

ParameterDescription
$postint|WP_Post. The attachment ID or object. Default is the current post.

Return Value

Return ValueDescription
trueThe attachment is an image.
falseThe attachment is not an image, or it was not found.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Check an ID

Test an attachment ID and print a message.

WordPress
<?php
if ( wp_attachment_is_image( 15 ) ) {
  echo 'Attachment 15 is an image.';
} else {
  echo 'Attachment 15 is not an image.';
}

How It Works

  1. 15 is the attachment ID.
  2. The function checks the file type.
  3. The message depends on the result.

Example 2 — Show an image only

Print the image only when the file is an image.

WordPress
<?php
$id = get_the_ID();
if ( wp_attachment_is_image( $id ) ) {
  echo wp_get_attachment_image( $id, 'medium' );
} else {
  echo '<p>This file is not an image.</p>';
}

How It Works

  1. The check protects wp_get_attachment_image() from non-image files.
  2. For a PDF we print a short message instead.
  3. This keeps the page clean.

Example 3 — Only list images

Go through the attachments of a post and keep the images.

WordPress
<?php
$files = get_attached_media( '', get_the_ID() );
foreach ( $files as $file ) {
  if ( wp_attachment_is_image( $file->ID ) ) {
    echo esc_html( get_the_title( $file->ID ) ) . "\n";
  }
}

How It Works

  1. get_attached_media() returns all files of the post.
  2. The if skips PDFs, videos, and other files.
  3. Only image titles are printed.

Frequently Asked Questions

It checks if an attachment in the Media Library is an image.
It returns true for an image, and false otherwise.
is_attachment() checks the page type. wp_attachment_is_image() checks the file type.
No, if you pass an attachment ID.
To avoid image functions on files that are not images, such as PDFs.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful