Disable images pages on WordPress in just 5 minutes

You want to prevent that single images can be called as individual page in your WordPress website? Then this little article is just right for you.

You can create a search engine friendly 301 redirect without any plugin and with only a few lines of code.

The following code ensures that attachments are redirected. First, it checks whether the attachment was uploaded for a page.

If the respective page could be identified, a forwarding to this page takes place. Otherwise, a redirection to the start page is performed.

You can conveniently add the following code to your theme’s Functions.php (ideally in the child theme).

/**
 Disable attachment page
 @see https://themeisle.com/blog/redirect-wordpress-attachment-pages/
 */
 function addRediretToAttachmentPage() {
 if ( is_attachment() ) {
     global $post;
 $url = esc_url( home_url( '/' )); if ( $post && $post->post_parent ) {     $url = esc_url( get_permalink( $post->post_parent )); } wp_redirect( $url , 301 ); exit;
 }
 }
 add_action( 'template_redirect', 'addRediretToAttachmentPage' ); 

By specifying the status code “301”, it is ensured that search engines also permanently update the indexed links or take them out of the index.

Note: It may take some time for search engines (Google, Bing…) to update your index.

I hope this article was helpful for you. Feel free to leave us a comment if you have any questions.

Hier klicken, um den Beitrag zu bewerten
[Gesamt: 0 Durchschnitt: 0]

Leave A Comment

Title