Attachment Pages Redirect - Version 1.1.2

Version Description

  • Changed code for 404 scenario to use the theme's 404 template if it's available.
Download this release

Release Info

Developer samuelaguilera
Plugin Icon wp plugin Attachment Pages Redirect
Version 1.1.2
Comparing to
See all releases

Code changes from version 1.1.1 to 1.1.2

Files changed (2) hide show
  1. attachment-pages-redirect.php +18 -4
  2. readme.txt +5 -1
attachment-pages-redirect.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Attachment Pages Redirect
4
  * Description: Makes attachment pages redirects (301) to post parent if any. If not, redirects (302) to home page.
5
  * Author: Samuel Aguilera
6
- * Version: 1.1.1
7
  * Author URI: http://www.samuelaguilera.com
8
  * License: GPLv3
9
  *
@@ -48,16 +48,30 @@ function sar_attachment_redirect() {
48
 
49
  $parent_post_in_trash = get_post_status( $post->post_parent ) === 'trash' ? true : false;
50
 
 
51
  if ( $parent_post_in_trash ) {
52
- wp_die( 'Page not found.', '404 - Page not found', 404 ); // Prevent endless redirection loop in old WP releases and redirecting to trashed posts if an attachment page is visited when parent post is in trash.
 
 
 
 
 
 
 
 
 
 
 
53
  }
54
 
55
- wp_safe_redirect( get_permalink( $post->post_parent ), ATTACHMENT_REDIRECT_CODE ); // Redirect to post/page from where attachment was uploaded.
 
56
  exit;
57
 
58
  } elseif ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && ( $post->post_parent < 1 ) ) {
59
 
60
- wp_safe_redirect( get_bloginfo( 'wpurl' ), ORPHAN_ATTACHMENT_REDIRECT_CODE ); // Redirect to home for attachments not associated to any post/page.
 
61
  exit;
62
 
63
  }
3
  * Plugin Name: Attachment Pages Redirect
4
  * Description: Makes attachment pages redirects (301) to post parent if any. If not, redirects (302) to home page.
5
  * Author: Samuel Aguilera
6
+ * Version: 1.1.2
7
  * Author URI: http://www.samuelaguilera.com
8
  * License: GPLv3
9
  *
48
 
49
  $parent_post_in_trash = get_post_status( $post->post_parent ) === 'trash' ? true : false;
50
 
51
+ // Prevent endless redirection loop in old WP releases and redirecting to trashed posts if an attachment page is visited when parent post is in trash.
52
  if ( $parent_post_in_trash ) {
53
+ $theme_404_template = locate_template( '404.php' );
54
+ if ( ! empty( $theme_404_template ) ) {
55
+ global $wp_query;
56
+ $wp_query->set_404();
57
+ status_header( 404 );
58
+ nocache_headers();
59
+ require get_404_template();
60
+ exit;
61
+ } else {
62
+ wp_die( 'Page not found.', '404 - Page not found', 404 );
63
+ exit;
64
+ }
65
  }
66
 
67
+ // Redirect to post/page from where attachment was uploaded.
68
+ wp_safe_redirect( get_permalink( $post->post_parent ), ATTACHMENT_REDIRECT_CODE );
69
  exit;
70
 
71
  } elseif ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && ( $post->post_parent < 1 ) ) {
72
 
73
+ // Redirect to home for attachments not associated to any post/page.
74
+ wp_safe_redirect( get_bloginfo( 'wpurl' ), ORPHAN_ATTACHMENT_REDIRECT_CODE );
75
  exit;
76
 
77
  }
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: samuelaguilera
3
  Tags: attachment, redirect, images, 301, 302
4
  Requires at least: 4.8
5
  Requires PHP: 5.6
6
- Tested up to: 5.2.2
7
  Stable tag: 1.1.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
@@ -50,6 +50,10 @@ define( 'ORPHAN_ATTACHMENT_REDIRECT_CODE', '301' );
50
 
51
  == Changelog ==
52
 
 
 
 
 
53
  = 1.1.1 =
54
 
55
  * Minor changes to make code 100% compliant with WordPress Coding Standards. This doesn't means any change in the plugin functionality, it's just code cosmetic.
3
  Tags: attachment, redirect, images, 301, 302
4
  Requires at least: 4.8
5
  Requires PHP: 5.6
6
+ Tested up to: 5.4
7
  Stable tag: 1.1.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
50
 
51
  == Changelog ==
52
 
53
+ = 1.1.2 =
54
+
55
+ * Changed code for 404 scenario to use the theme's 404 template if it's available.
56
+
57
  = 1.1.1 =
58
 
59
  * Minor changes to make code 100% compliant with WordPress Coding Standards. This doesn't means any change in the plugin functionality, it's just code cosmetic.