title: How disable image attachment pages in WordPress?
body: <p>I am trying to disable image attachment pages in my WordPress site as it the images are stumbling my rankings. I did a research and found I can do it by implementing a code in my image.php file instead of installing a plugin.</p>
  
  <p>I really need your help as I don't know where to place the code in the image.php file.</p>
  
  <p>The code I want to add: <code>&lt;?php wp_redirect(get_permalink($post-&gt;post_parent)); ?&gt;</code></p>
  
  <p>My image.php file: </p>
  
  <pre><code>&lt;?php
  /**
   * The template for displaying image attachments.
   *
   * @package sparkling
   */
  
  get_header();
  ?&gt;
  
              &lt;?php while ( have_posts() ) : the_post(); ?&gt;
                  &lt;div class="post-inner-content"&gt;
                      &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt;
                          &lt;header class="entry-header"&gt;
                              &lt;h1 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
  
                              &lt;div class="entry-meta"&gt;
                                  &lt;?php sparkling_posted_on(); ?&gt;
                              &lt;/div&gt;&lt;!-- .entry-meta --&gt;
  
                              &lt;nav role="navigation" id="image-navigation" class="navigation-image nav-links"&gt;
                                  &lt;div class="nav-previous"&gt;&lt;?php previous_image_link( false, __( '&lt;i class="fa fa-chevron-left"&gt;&lt;/i&gt; Previous', 'sparkling' ) ); ?&gt;&lt;/div&gt;
                                  &lt;div class="nav-next"&gt;&lt;?php next_image_link( false, __( 'Next &lt;i class="fa fa-chevron-right"&gt;&lt;/i&gt;', 'sparkling' ) ); ?&gt;&lt;/div&gt;
                              &lt;/nav&gt;&lt;!-- #image-navigation --&gt;
                          &lt;/header&gt;&lt;!-- .entry-header --&gt;
  
                          &lt;div class="entry-content"&gt;
  
                              &lt;div class="entry-attachment"&gt;
                                  &lt;div class="attachment"&gt;
                                      &lt;?php
                                          /**
                                           * Grab the IDs of all the image attachments in a gallery so we can get the URL of the next adjacent image in a gallery,
                                           * or the first image (if we're looking at the last image in a gallery), or, in a gallery of one, just the link to that image file
                                           */
                                          $attachments = array_values( get_children( array(
                                              'post_parent'    =&gt; $post-&gt;post_parent,
                                              'post_status'    =&gt; 'inherit',
                                              'post_type'      =&gt; 'attachment',
                                              'post_mime_type' =&gt; 'image',
                                              'order'          =&gt; 'ASC',
                                              'orderby'        =&gt; 'menu_order ID'
                                          ) ) );
                                          foreach ( $attachments as $k =&gt; $attachment ) {
                                              if ( $attachment-&gt;ID == $post-&gt;ID )
                                                  break;
                                          }
                                          $k++;
                                          // If there is more than 1 attachment in a gallery
                                          if ( count( $attachments ) &gt; 1 ) {
                                              if ( isset( $attachments[ $k ] ) )
                                                  // get the URL of the next image attachment
                                                  $next_attachment_url = get_attachment_link( $attachments[ $k ]-&gt;ID );
                                              else
                                                  // or get the URL of the first image attachment
                                                  $next_attachment_url = get_attachment_link( $attachments[ 0 ]-&gt;ID );
                                          } else {
                                              // or, if there's only 1 image, get the URL of the image
                                              $next_attachment_url = wp_get_attachment_url();
                                          }
                                      ?&gt;
  
                                      &lt;a href="&lt;?php echo $next_attachment_url; ?&gt;" title="&lt;?php the_title_attribute(); ?&gt;" rel="attachment"&gt;&lt;?php
                                          $attachment_size = apply_filters( 'sparkling_attachment_size', array( 1200, 1200 ) ); // Filterable image size.
                                          echo wp_get_attachment_image( $post-&gt;ID, $attachment_size );
                                      ?&gt;&lt;/a&gt;
                                  &lt;/div&gt;&lt;!-- .attachment --&gt;
  
                                  &lt;?php if ( ! empty( $post-&gt;post_excerpt ) ) : ?&gt;
                                  &lt;div class="entry-caption"&gt;
                                      &lt;?php the_excerpt(); ?&gt;
                                  &lt;/div&gt;&lt;!-- .entry-caption --&gt;
                                  &lt;?php endif; ?&gt;
                              &lt;/div&gt;&lt;!-- .entry-attachment --&gt;
  
                              &lt;?php the_content(); ?&gt;
                              &lt;?php
                                  wp_link_pages( array(
                                      'before' =&gt; '&lt;div class="page-links"&gt;' . __( 'Pages:', 'sparkling' ),
                                      'after'  =&gt; '&lt;/div&gt;',
                                  ) );
                              ?&gt;
  
                          &lt;/div&gt;&lt;!-- .entry-content --&gt;
  
                          &lt;footer class="entry-meta"&gt;
                          &lt;/footer&gt;&lt;!-- .entry-meta --&gt;
                      &lt;/article&gt;&lt;!-- #post-&lt;?php the_ID(); ?&gt; --&gt;
                  &lt;/div&gt;
                  &lt;?php
                      // If comments are open or we have at least one comment, load up the comment template
                      if ( comments_open() || '0' != get_comments_number() )
                          comments_template();
                  ?&gt;
  
              &lt;?php endwhile; // end of the loop. ?&gt;
  
  &lt;?php get_sidebar(); ?&gt;
  &lt;?php get_footer(); ?&gt;
  </code></pre>