Ok so I’ve been doing a few projects recently involving WordPress. In general I quite like it and I can manage to steer clear of most plugins to get a skin to work. One of the features I have been using quite a lot recently is the featured image, or post thumbnail. Its great as I often find that pages that can be content managed also have a nice graphic attached. Have a look through the codex linked for examples of how to use it in a theme.
One thing which I don’t like about the feature tho is the fact that although you can supposedly set the attachment link in the media lightbox it doesn’t actually come through in the theme. In fact I have found that more often than not if you upload an image and then subsequently want to change the link it isn’t very easy to do at all, and more often than not cannot be set. This seems to apply to all images whether they are feature or in a gallery. After a lot of frustration and Google searching it seems it is a low priority bug. Even worse it seems to have a patch, but it hasn’t made it into the core yet. Anyway using this patch I was not only able to fix the link not updating I was also able to figure out how to then get that link back out to use with a featured image.
Ok first the patch. All you need to do here is copy the following code into a file called save_image_url.php in your plugins folder, then activate the plugin:
<?php
/*
Plugin Name: Save Custom Link URL
Version: 0.1
Plugin URI: http://core.trac.wordpress.org/ticket/13429
Description: Allows to specify a custom Link URL for any attachment.
Author: Sergey Biryukov
Author URI: http://profiles.wordpress.org/sergeybiryukov/
*/
function _save_attachment_url($post, $attachment) {
if ( isset($attachment['url']) )
update_post_meta( $post['ID'], '_wp_attachment_url', esc_url_raw($attachment['url']) );
return $post;
}
add_filter('attachment_fields_to_save', '_save_attachment_url', 10, 2);
function _replace_attachment_url($form_fields, $post) {
if ( isset($form_fields['url']['html']) ) {
$url = get_post_meta( $post->ID, '_wp_attachment_url', true );
if ( ! empty($url) )
$form_fields['url']['html'] = preg_replace( "/value='.*?'/", "value='$url'", $form_fields['url']['html'] );
}
return $form_fields;
}
add_filter('attachment_fields_to_edit', '_replace_attachment_url', 10, 2);
?>
This will mean you can save your custom link URLs set in the media light box. Then to use them with your featured image use the following code in your theme:
<?php if(has_post_thumbnail()): ?>
<a href="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_url', true ); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
If you also want to have a gallery which links the slides to different pages then you can use the following code:
<div id="carousel">
<?php
$attachments = get_children(array(
'post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
echo '<ul>';
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'carousel-slide' );
$image_link = get_post_meta( $attachment->ID, '_wp_attachment_url', true );
echo '<li class="carousel-slide">';
echo '<a href="'.$image_link.'"><img src="'.$image_attributes[0].'" width="'.$image_attributes[1].'" height="'.$image_attributes[2].'"></a>';
echo '<h2>'.$attachment->post_title.'</h2>';
echo '<p>'.$attachment->post_content.'</p>';
echo '</li>';
}
echo '</ul>';
?>
</div>
Modify to your requirements. It seems a shame to me that there was no obvious way of getting the link URL out – something along the lins of wp_get_attachment_image_link(); but no. To get the link you need get_post_meta( $id, ‘_wp_attachment_url’, true ); which isn’t obvious at all.
Wow just spent the past 6 hours trying to come with a suitable solution for finding the wp_get_attachment_image_src .
Your last selection solves the problem perfectly. Thank you so much!
THANK YOU!
I’ve been wracking my brain and searching for this solution as well. Works great! I’m a very light programmer — I had found the patch, but didn’t understand all the syntax or how to actually pull out the URL in my theme.
Can’t imagine how many other people are struggling with this. Thanks for a straight forward explanation and fix.