Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When doing a bit of Google searching for content on our blog, I noticed to my shock and horror that individual images from the Media Library are somehow generating their own URLs that Google is somehow finding and indexing!

For example this page:
http://blog.stackoverflow.com/2008/08/special-development-team-podcast/

Contains this image:
http://blog.stackoverflow.com/wp-content/uploads/bio-jarrod-dixon.jpg

Which is fine, but somehow this image is also exposed as its own URL and "post":
http://blog.stackoverflow.com/2008/08/special-development-team-podcast/bio-jarrod-dixon/

This is extremely unwanted!

I checked the Media settings in WordPress and browsed around the Media Library but I can't figure out a way to disable this behavior. Any ideas?

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

This thing you are saying is unwanted is just normal functionality under WordPress and it cannot be removed. However there are things you can do to point the unwanted URL to something more usefull.

Here is a forum post on this issue with some interesting fixes and a description on what is happening:

http://wordpress.org/support/topic/disable-attachment-posts-without-remove-the-medias

Attachments are actually a post type, so they take a row in the posts table like a post does, they will always have a URL available, in the same way that posts do to..

ie. example.com/?p=16

16 is the post ID, and like posts they will always be available by a URL like the above. Media files aren't simply considered files, they have a more content like element to them, in that they have a record in the posts table that corresponds to them, just like a post or page does.

What you're asking is how to stop the automatic existance of individual attachment URLs for each media item(not really possible because they're essentially a post type, which means they'll always be a URL for them).

Here's a suggestion though, take any template(theme) file, index.php, page.php, archive.php or whatever you like, create a copy and rename it to image.php or attachment.php if you want to target all media. Open the file, remove the loop, save... and load up one of the attachment pages(like the one you provided before)..

My point being, all you need to do is create an attachment template file: http://codex.wordpress.org/Template_Hierarchy
http://codex.wordpress.org/Template_Hierarchy#Attachment_display

If you wanted to, in theory you could place a redirect into the attachment template so individual attachment views are redirected (or any number of other things you might want to do).

Someone posted just that, an attachment.php that goes in your /themes folder to redirect:

<?php
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.get_permalink($post->post_parent));
?>
share|improve this answer
    
Thank you Jeff for adding to my answer :) –  Brady Aug 7 '11 at 8:51
add comment

I figured it's about time i at least tried my hand at wiping out attachment pages.

Here's my first shot at it...

add_filter( 'attachment_fields_to_edit', 'wpse_25144_attachment_fields_to_edit', 10000, 2 );

function wpse_25144_attachment_fields_to_edit( $form_fields, $post ) {

    $url_type = get_option( 'image_default_link_type' );

    if( 'post' == $url_type ) {
        update_option( 'image_default_link_type', 'file' );
        $url_type = 'file';
    }

    $form_fields['url'] = array(
        'label'      => __('Link URL'),
        'input'      => 'html',
        'html'       => wpse_25144_image_link_input_fields( $post, $url_type ),
        'helps'      => __('Enter a link URL or click above for presets.')
    );

    return $form_fields;
}

function wpse_25144_image_link_input_fields($post, $url_type = '') {

    $file = wp_get_attachment_url($post->ID);

    if( empty( $url_type ) )
        $url_type = get_user_setting( 'urlbutton', 'file' );

    $url = '';
    if( $url_type == 'file' )
        $url = $file;

    return "
    <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
    <button type='button' class='button urlnone' title=''>" . __('None') . "</button>
    <button type='button' class='button urlfile' title='" . esc_attr($file) . "'>" . __('File URL') . "</button>
";
}

add_filter( 'query_vars', 'wpse_25144_query_vars', 10000, 2 );

function wpse_25144_query_vars( $wp_query_vars ) {

    foreach( $wp_query_vars as $i => $qv ) {
        if( in_array( $qv, array( 'attachment', 'attachment_id' ) ) )
            unset( $wp_query_vars[$i] );
    }
    return $wp_query_vars;
}

add_filter( 'attachment_link', 'wpse_25144_attachment_link', 10000, 2 );

function wpse_25144_attachment_link( $link, $id ) {

    $link = wp_get_attachment_url( $id );
    return $link;
}

add_filter( 'rewrite_rules_array', 'wpse_25144_rewrite_rules_array', 10000 );

function wpse_25144_rewrite_rules_array( $rewriteRules ) {

    foreach( $rewriteRules as $pattern => $query_string ) {
        if( false === strpos( $pattern, 'attachment' ) && false === strpos( $query_string, 'attachment' ) )
            continue;
        unset( $rewriteRules[$pattern] );
    }

    return $rewriteRules;
}

Removes the attachment rewrites, updates the attachment links to point at the attachment file(instead of it's permalink), removes the attachment query vars and also removes the ability to link attachments to the now non-existant attachment permalink.

Open to critique. :)

share|improve this answer
add comment

You can do a 301 redirection for attachments to their parent's page like this:

<?php
/*
Plugin Name: Redirect Attachments to Parent (301)
Plugin URI: http://wordpress.stackexchange.com/questions/25144/unwanted-media-library-urls-in-posts
Description: Redirect any attachemnt pages to their parent's page with 301 redirection
Author: Ashfame
Version: 0.1
Author URI: http://www.ashfame.com/
*/

add_action( 'template_redirect', 'attachment_post_type_redirection' );

function attachment_post_type_redirection() {
    global $wp_query;       
    if ( is_attachment() ) {            
        wp_redirect( get_permalink( $wp_query->post->post_parent ), 301 );
    }       
}
share|improve this answer
add comment

Yoast's SEO plugin has "Redirect attachment URL's to parent post URL" under permalinks. I used this option to resolve the issue. The plugin is amazing.

share|improve this answer
    
good idea, but not sure I want a whole multi-function plugin to solve this one particular problem.. –  Jeff Atwood Aug 6 '11 at 22:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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