• Resolved Ganners

    (@ganners)


    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

Viewing 15 replies - 1 through 15 (of 19 total)
  • Thread Starter Ganners

    (@ganners)

    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

    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

    Thread Starter Ganners

    (@ganners)

    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.

    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!

    Thread Starter Ganners

    (@ganners)

    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.

    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.

    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.

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

    Woot! ??

    J.

    Anonymous User 154007

    (@anonymized-154007)

    Isn’t that the job of the url_to_postid() method ? See: https://codex.www.ads-software.com/Function_Reference/url_to_postid

    Thread Starter Ganners

    (@ganners)

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

    Anonymous User 154007

    (@anonymized-154007)

    @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.

    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.

    Anonymous User 154007

    (@anonymized-154007)

    @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: https://github.com/kdeldycke/e107-importer/commit/a2fa4280acc9c7d5ad721351f0af172269dd0224

    Kevin,

    Rad! And thank you for the update — I’ll definitely check out your plugin when I get a moment. ??

    J.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Need to get attachment ID by Image URL’ is closed to new replies.