Removing the link of image in WordPress attachment page

Last updated on November 28, 2011. Tags: , ,

Whenever you embed an image to your post or static page, a small window will appear at the center and darken the rest of your dashboard. You're then prompted to either download the image or select from among those you already downloaded (media library). Then you will be prompted to configure it in a certain manner like encoding the title attribute, alt text and link URL - the link to which you will be forwarded when you click on the image.

Under the link URL, you have three quick options: none, file URL and post URL. None means your image will just be a simple embedded image and not a hyperlink. File URL will link the image to its actual URL (or the URL of its full size in case you didn't choose "full size" under the size option) and you will see the image alone in your browser window. Post URL will link the image to an attachment page, wherein you will see a page similar to the post and static page of your theme, but it contains only the image and nothing else.

wordpress add image option

By default, the attachment page will display the "medium" size of the image (the size options thumbnail, medium and large can be configured in Settings ยป Media, full size is the actual image size) and this medium size image, in turn is a hyperlink to the actual URL of the full size image. To change the default medium size, click here. For this post, we will deal with the default hyperlink image in the attachment page.

Suppose you don't want this image to be a hyperlink. Maybe you do not want the image to be seen without the look and feel of your site. Perhaps, the idea of having a large solo image on a page and still being a hyperlink does not appeal to you. Whatever your reason, you can make the attachment page display only that image without it being a hyperlink.

Unfortunately, this option is not available in the Dashboard. You need to do some code tweaking to implement this.

First, you need to open post-template.php using the text editor provided in your hosting account (or download and open it using notepad). You can find it in the /wp-include/ folder in your root directory, or in whatever folder you installed WordPress.

Then, look for the line:

return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );

Delete the entire code above and replace it with the following:

if (is_attachment())
{return apply_filters( 'wp_get_attachment_link', "$link_text", $id, $size, $permalink, $icon, $text ); }
else
{return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text ); }

Then save it, or if you are using notepad, save it and upload it and overwrite the original file.

That's it. If you have an attachment page, you can refresh it and you will see that the image embedded on it is no longer a hyperlink. If you have none, then upload an image and select the post URL option. The image embedded on the page or post is a link to an attachment page. The image on the attachment page, however, is no longer a link.

Important notes:

  • The replaced line seems to control not just the link on the attachment page, but also the embedded picture in static pages and posts in case they were set to link to the file or the attachment page. The if-argument was added to ensure that the changes we made will apply only to the attachment page.
  • The file /wp-include/post-template.php is a core WordPress file and not part of the template. If you upgrade your WordPress version, you need to reimplement this tweak.
  • This tweak was tested on WordPress 2.7.1 and 2.9.2. It is likely to work in all near versions and all versions in between.

Posted by Greten on June 8, 2010 under WordPress

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by Carl on 12.11.10 7:55 am

    Thanks for this fix, it has made me very happy after days of looking through the theme code.

  2. Posted by Azzam on 01.19.11 12:12 pm

    Outstanding!

    I have a blog that has 100′s of post and want to include into Google News.

    How every Google does not allow images to be click-able [probably for Google images and thumbnails]

    So needed an easy solution instead of doing it manually.

    Thank you so much for saving me all that time.

  3. Posted by Greten on 02.02.11 2:37 pm

    You’re welcome guys : )

    @Azzam – I’m not sure how this tweak can be useful in Google News. Care to show us the site?

  4. Posted by kyle on 04.11.11 1:43 am

    How do you do the same thing for setting post images to having no url link as default

  5. Posted by Greten on 05.18.11 10:53 am

    @Kyle – see the screenshot? Just click on the “None” and “Link URL” will be blank. This article covers the attachment pages, not the post pages.

  6. Posted by Harsh Agrawal on 09.29.11 8:01 pm

    Tried but it didn’t worked.Using WordPress 3.2 …

  7. Posted by Greten on 01.21.12 5:18 am

    Thank you for the information Harsh. I will look at it as soon as possible even though I haven’t used this method myself for a long time.

  8. Posted by Will on 01.24.12 2:34 am

    I wrote a plugin which will fix the link as an image without modifying core files :) It works from version 2.5+

    http://wordpress.org/extend/plugins/remove-link-url/

    Thanks for the nice writeup describing this annoying problem that WordPress poses.

  9. Posted by Greten on 01.24.12 11:44 am

    Thanks Will, I’ll take a look at your plugin sometime later. While I prefer solutions without plugin, I also prefer solutions that does not in anyway involve rewriting the core files.

Post Comments





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.