Get post attachment images dimension and use in embed code

I am trying to make things work like where any post attachment image will generate embed code automatically.

Example: If I upload and add two images 1. 250×150 2. 600 x 160 and insert into post. Now that image information I want to auto put into embed code like below.

Read More
<div>
<a href="link url" title="title text" target="_blank"><img src="$image_url" width="$image_width" height="$image_height" alt="$image_name" /></a>
</div>

where $variable is the data of the attached image/s.

So above code our team member can use the embed code to display image.

Related posts

Leave a Reply

1 comment

  1. WordPress has a predefined function, wp_get_attachment_image_src that returns the following information as an array:

    [0] => url
    [1] => width
    [2] => height
    

    But, the function requires that you know the attachment’s ID, not the post’s ID. In order to get that, you’ll need to use get_post_thumbnail_id.

    Here’s the two functions together in action:

    $image_id = get_post_thumbnail_id($post->ID);
    $image = wp_get_attachment_image_src($image_id);
    

    After that, you can easily do what you’re talking about in your question.

    <img src="$image[0]" width="$image[1]" height="$image[2]" />
    

    If you want more information about the wp_get_attachment_image_src, you can read more about it here: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src.