[wp-hackers] Newbie Q - can't get get_children to work

Gaarai gaarai at gaarai.com
Sat Apr 11 23:25:03 GMT 2009

Most current blog editors are able to embed images just fine without 
requiring this workaround. For example, if you use Raven 
(http://www.zoundryraven.com/), the software will automatically upload 
the images first, replace the img tag parameters with data returned from 
the blog, and then publish the post.

Sticking with the topic as you may have your own reasons for pursuing 
this path... Just so you know, doing a lookup by file name is 
error-prone. File attachments do not have to have unique file names. If 
you upload the same file name back-to-back, the second upload will have 
a digit added just before the extension in order to make it unique. 
However, if files with the same name are uploaded during different 
months or years, the duplicate file names are allowed.

Of course, this behavior changes depending on the specific settings of 
the blog in question. For example, if the "Organize my uploads into 
month- and year-based folders" option is disabled, files will be given 
unique names.

With this limitation in mind, I've produced a very rough working 
function that will do what you want. The code is as follows:

    function get_image_from_file_name( $file_name, $size = 'medium' ) {
        global $wpdb;   

        // Protect against db exploits
        $file_name = addslashes( $file_name );   

        // Find the relevant record.
        // Since attachments are not guaranteed to have unique names,
        // find the oldest match. This prevents existing images from
        // being replaced accidentally by another image that is
        // uploaded with the same name.
        $image_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_type='attachment' AND guid LIKE '%/$file_name' ORDER BY post_date LIMIT 1" );    

        return image_downsize( $image_id, $size )

An example of using this code is as follows:

    list( $image_src, $image_width, $image_height ) = get_image_from_file_name( 'my-dog.png', 'medium' );
    if ( ! empty( $image_src ) )
        echo "<img src='$image_src' width='$image_width' height='$image_height' alt='My Dog' />";

The default sizes offered by WP are thumbnail, medium, large, and full. 
These can be modified by plugins however.

Since running the function for each image that needs to be rendered on a 
page is expensive, I recommend using the function that I've provided 
here and latching a filter onto wp_insert_post_data that replaces the 
short tag that you had in a previous message with an actual img tag. 
Doing this will save a ton of needless db queries and could potentially 
simplify your template code.

If you end up creating this filter, the get_image_from_file_name 
function could be improved to first look for a match that has a post 
date close to that of the actual post and then reverting to the original 
functionality if one is not found. This would allow for using duplicate 
file names.

I hope that this helps you.

Chris Jean

Jan Erik Moström wrote:
> On 09-04-11 at 21.15, Gaarai <gaarai at gaarai.com> wrote:
>> Am I correct in that you simply need to get the attachment details 
>> given a specific file name?
> What I need to be able to get is
> +   the different size versions for one image
> +   get url for those versions
> +   get image and height for those versions
> I have no need to write or in any way modify anything, just reading.
> And the only thing I have as input is the URL (or filename) to the 
> original image.
>                 jem

More information about the wp-hackers mailing list