0

I've noticed that if I upload an image to the media library it is given a post_name in the database and is then accessible via mysite.com/wordpress/its-post_name. It only seems to do it if I upload it via media > add new. If it's added to the post via 'set featured image' it doesn't do it.

This could be a problem if visitors type in a url expecting to find an article, but only discover the corresponding image. Is there any way to block access to this type of image-only 'post'?

Is it supposed to do this or is it some kind of bug?

  • That is probably the url of the attachment and not the image, and if it is actually the url of the image then something is most likely wrong there. The url of the attachment by itself follows the rules you setup for your permalink structure. You should probably add info here about the url of the site and the permalink structure you use but it doesn't feel like there is anything wrong here. – Mark Kaplun Jul 17 '15 at 15:26
  • Yes it's the attachment url, not the image directory (e.g. uploads/2015/07). But still, it seems to create a 'post' with no content other than the image and theme. I wouldn't expect it to do this and it doesn't if I upload it via 'set feaured image'. – CharlesStamp Jul 17 '15 at 15:32
  • sounds like there is something you are missing. Each image is an attachment and therefor should have an attachment url. If your featured image was uploaded the "normal" way then you should be able to see it in the media library and from there have access (and change IIRC) the attachment page url. – Mark Kaplun Jul 18 '15 at 3:29
1

You are most likely looking at the attachment page when visiting the url. It is default Wordpress behavior to create a page for every image you upload. If you don't want these attachment pages to be accessible, you can block access to it.

To do this, edit the file attachment.php (or image.php if you want to just block access to the images) in your theme or add this file if it doesn't exists. Add the following line of code as the first lines. All visitors of this attachment page will now be redirected to either the post where the image is connected to or, as a fallback, to your homepage.

<?php
  global $post;
  if( $post->post_parent != 0 ) {
     $redirect_url = get_permalink($post->post_parent);
  } else {
     $redirect_url = get_bloginfo('url');
  }
  wp_redirect($redirect_url);
  exit();
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.