WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need to get attachment ID by Image URL (20 posts)

  1. Ganners
    Member
    Posted 3 years ago #

    I'm using a jquery script called Captify, it works by taking the alt tag of an image and displaying it as the alt tag on mouse over.

    I'm also using the header image theme plugin and this has no option to add or display the alt tag, and that's fine as I can add the alt in the media browser.

    What I therefore need is a way to get the attachment ID of the header image and display its alt.

    The code below works, but this means I have to set the attachment ID manually (602). I'm sure my client wouldn't want to do this so I'd like a way to make it dynamic. Are there any already-existing functions that I'm missing or should I create my own query?

    <img src="<?php header_image(); ?>"
    class="captify"
    width="<?php echo HEADER_IMAGE_WIDTH; ?>"
    height="<?php echo HEADER_IMAGE_HEIGHT; ?>" 
    
    alt="
    <?php
    $alt_text = get_post_meta(602, '_wp_attachment_image_alt', true);print $alt_text;
    ?>
    " />

    and the test site:
    VC News

    Thanks for any help!

    Mark

  2. Ganners
    Member
    Posted 3 years ago #

    I did the only solution I could think of, but I'm sure it must be considered quite ugly. If any of you know a cleaner way to achieve this then I'd love to know it.

    Anyway the following code will allow your image header to have an alt tag if it is set in your Media Library:

    <img src="<?php header_image(); ?>" class="captify" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="
    <?php
    
    $HeaderImageURL = get_header_image();
    $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT *
    FROM $wpdb->posts WHERE post_content = '$HeaderImageURL'" ) );
    
    $theID = $thepost->ID;
    $theTitle = $thepost->post_title;
    
    $alt_text = get_post_meta($theID, '_wp_attachment_image_alt', true);
    if ($alt_text == NULL)
    {
    	$alt_text = "Please add some Alternate Text to $theTitle in the 'Media > Library' section of your admin panel";
    }
    print $alt_text;
    ?>
    " /></a>

    and it can be viewed in action at VC News

    Cheers

  3. maorb
    Member
    Posted 3 years ago #

    I'm just looking for the same solution. I assume your will work, but as you said I think should be a cleaner way to do it.
    I'll be happy to know if anyone finds..

    Thanks

  4. Ganners
    Member
    Posted 3 years ago #

    I've searched around this subject a lot and I've found there is no simpler way, a simple custom query works fine and isn't so bad! Otherwise slash at the header script or make your own plugin.

    I have modified it slightly and have this:

    <img src="<?php header_image(); ?>" class="captify" style="border:1px solid #333;" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="
    <?php
    $HeaderImageURL = get_header_image();
    $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT *
    FROM $wpdb->posts WHERE post_content = '$HeaderImageURL'" ) );
    $theID = $thepost->ID;
    $theTitle = $thepost->post_title;
    $alt_text = get_post_meta($theID, '_wp_attachment_image_alt', true);
    
    if ($alt_text == NULL)
    {
    	$alt_text = "Please add some Alternate Text to $theTitle in the 'Media > Library' section of your admin panel";
    }
    
    if ($theID == NULL)
    {
    	$alt_text = "";
    	bloginfo('name');
    	print " | ";
    	bloginfo('description');
    }
    
    print $alt_text;
    ?>" />

    A nice way is to make a plugin for it, I'll probably create it within the next month anyway.

  5. brianfreytag
    Member
    Posted 3 years ago #

    I had this need as well but for a different purpose.

    I'm using the Media Tags plugin to allow users to add the "banner" tag to a graphic that they upload and then select it in the Theme Options panel for their front page.

    I was able to extract the URL of the graphic using the Media Tags plugin and then used that database entry to create the image src tag:

    <?php $banner_image = get_option('extension_banner'); ?>

    <img src="<?php echo $banner_image; ?>" />

    This works fine, but where I work, we need to be ADA (Americans with Disabilities Act) Compliant with all of our websites, so I needed proper alt and title tags.

    Because this is a header image and is outside of "the loop," I couldn't use your query as you wrote it... This is what I came up with:

    <?php
    	$banner_image = get_option('extension_banner');
    	$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT *
    	FROM $wpdb->posts WHERE guid = '$banner_image'" ) );
    
    	$theID = $thepost->ID;
    	$banner_title = $thepost->post_title;
    	?>
    
    	<img src="<?php echo $banner_image; ?>" alt="<?php echo $banner_title; ?>" title="<?php echo $banner_title; ?>" />

    In the guid in the posts table is the URL to the actual graphic. Because the graphic isn't in a post, there is no "post_content," so I select using the guid URL and the post_title which is the Title of the image.

    Hope this helps other people!

  6. Ganners
    Member
    Posted 3 years ago #

    If you're looking to be able to put this information in inside your header admin panel, this will do that. It will add 2 text boxes for caption and for alt text. If you want this then open up wp-admin/custom-header.php and replace your code with this (from pastebin).

    Then to use this code you need to use the code above to grab these.

  7. brianfreytag
    Member
    Posted 3 years ago #

    Ganners,

    I had originally anticipated doing something like that, but people hate typing. So I figured it'd be easier to not make them type anything more except for after they upload their banner.

    Just pull the the banner info from the guid in the posts table, and I can do whatever I want with it.

    Harder for me, easier for the end user. That's good business.

  8. jameslafferty
    Member
    Posted 3 years ago #

    Here's what I came up with by way of encapsulating get_attachment_id_from_src:

    function get_attachment_id_from_src ($image_src) {
    
    		global $wpdb;
    		$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
    		$id = $wpdb->get_var($query);
    		return $id;
    
    	}

    I'm noting this b/c I came across this thread looking to do something somewhat different, and think this function might prove useful to anyone looking specifically to get the attachment id from the image source.

    This returns the id of the attachment if successful or NULL if unsuccessful.

    Cheers,

    J.

  9. manuelmasia
    Member
    Posted 3 years ago #

    Thank you James!! I saved a lot of time with your function!

  10. jameslafferty
    Member
    Posted 3 years ago #

    Woot! :)

    J.

  11. Kevin Deldycke
    Member
    Posted 3 years ago #

    Isn't that the job of the url_to_postid() method ? See: http://codex.wordpress.org/Function_Reference/url_to_postid

  12. Ganners
    Member
    Posted 3 years ago #

    Thanks for the link Kevin. These functions aren't easy to find! Guess my googling isn't up to scratch :)

  13. Kevin Deldycke
    Member
    Posted 3 years ago #

    @Ganners: Be carefull, I just uncovered a bug about url_to_postid(). As described in this ticket, it may not resolve some attachement URLs in some special cases.

  14. jameslafferty
    Member
    Posted 3 years ago #

    Kevin,

    That function works for finding the post id based on the attachment url, but not for finding the post id image url. The obvious caveat for my solution is that it isn't going to work on custom image sizes.

    I'm trying to remember the scenario where I didn't have the attachment url, but did have the image url, and it's not coming to me... Anyhow, I'm totally with you that it's best to use the WP functions when they're available, but for this particular case (very particular), I don't think there is a function. Of course, I might be totally wrong, in which case, egg on my face :).

    Cheers,

    J.

  15. Kevin Deldycke
    Member
    Posted 3 years ago #

    @jameslafferty: you're right, we can distinguish image URLs and attachment URLs. I explicited this in my bug report. I also reported there your solution.

    But now, when you take a closer look at how methods are named in the WordPress framework, some inconsistencies emerges and it doesn't look like image URLs and attachment URLs are distinguished. See my comments in my bug report.

    As for the use case of getting attachment ID from an image URL, I encounter this situation while working on my e107 Importer plugin. In it I used media_sideload_image() to upload a remote image. Unfortunately this method return an HTML tag. So I had to parse and extract the image URL from it then get back its corresponding attachment ID (that's where I used your get_attachment_id_from_src() method). See my corresponding commit here: http://github.com/kdeldycke/e107-importer/commit/a2fa4280acc9c7d5ad721351f0af172269dd0224

  16. jameslafferty
    Member
    Posted 3 years ago #

    Kevin,

    Rad! And thank you for the update -- I'll definitely check out your plugin when I get a moment. :)

    J.

  17. Gustavo Bordoni
    Member
    Posted 3 years ago #

    Hi Guys,

    I've got an solution that may be usefull, because I've got some problems using the GUID as my search string sometimes I've got duplicated content, I don't know why but I got it, so I developed this function to return any Post ID based in it's url:

    function drfw_postID_by_url($url) {
    	global $wpdb;
    	$id = url_to_postid($url);
    	if($id==0) {
    		$fileupload_url = get_option('fileupload_url',null).'/';
    		if (strpos($url,$fileupload_url)!== false) {
    			$url = str_replace($fileupload_url,'',$url);
    			$id = $wpdb->get_var("SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '$url' AND wposts.post_type = 'attachment'");
    		}
    	}
    	return $id;
    }

    So drfw is just a prefix im using, if you guys have any problems using this functionlet me know so I can improve it.

    Thanks,

  18. robcolburn
    Member
    Posted 3 years ago #

    Thanks webord,

    Very helpful right now. BTW: you may want to escape the url. For my purposes, I needed to.

    $id = $wpdb->get_var($wpdb->prepare("SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $url));

  19. Gustavo Bordoni
    Member
    Posted 3 years ago #

    Very good Idea Robcolburn,

    I've just forgot to prepare the data, my fault!

    But thanks.

  20. Burke Ingraffia
    Member
    Posted 3 years ago #

    I would just like to see in the post gallery somewhere that says "Attachment ID = X." Is this possible?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.