I would like to get the images from the_content(), I know I could use a regex to get the images but I wanted to know if there is any other more 'wordpress way' for doing this?

I have this within a plugin and I have a filter

 add_filter('the_content', 'myTestContentFunction');

Thank you

  • If the images you are trying to retrieve were added via the media uploader, a better & more WordPress way of getting the images would be to use the get_children function with the post_type parameter set to 'attachments'.
    – Will
    Jul 10 '14 at 11:27

There are essentially three criteria here:

  • do images belong to the site (are WP attachments)
  • are images attached to this post
  • are images used in this post

This has side effects, like:

  • images used in post are not necessarily attachments
  • post's attachments aren't necessarily used in that post

So the only reliable way to produce complete set of images, actually used in specific post's content is indeed to parse the content (after shortcodes like galleries and embeds) for them.

After that you can [try] reverse engineer URLs into WP's attachments, see Turn a URL into an Attachment / Post ID for example.

  • I can detect if the images are wp_attachements with ease, then but my problem is to have something out of the box for this. For e.g.: [gallery ids="10,11,12"], I was unable to find any hook that can give me the ids without me doing a regex or somehing to get the string and then do an explode or something. I don't see this a very good approach to do things, I consider it more of a 'hack'. I like to avoid hacks as much as possible. Jul 10 '14 at 11:43
  • @MariusTalagiu you don't have a "clean" choice here. It's "unreliable" (querying post's attachments), "hack" (parsing content), or nothing.
    – Rarst
    Jul 10 '14 at 11:46
  • Yeah, that's what I have figured as well, but I thought there is something out there that I have missed. Thank you Jul 10 '14 at 11:48

You can use the get_attached_media(), function. I have linked the above to the Codex.

P.S This is not my own code, straight from the WordPress Codex:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID

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

 endwhile; endif; ?>


You could try something like this:

function get_attached_images() {
global $post;
$post_id = $post->ID;
$media = get_attached_media( 'image', $post_id );
  • Since this is inside of a plugin I can not do the have_post(). But I have something in mind based on your answer, thank you. I will give it a shot and get back with a reply. Jul 10 '14 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.