Version Description
- 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.
Download this release
Release Info
Developer | samuelaguilera |
Plugin | Attachment Pages Redirect |
Version | 1.1.1 |
Comparing to | |
See all releases |
Code changes from version 1.1 to 1.1.1
- attachment-pages-redirect.php +23 -20
- readme.txt +7 -3
attachment-pages-redirect.php
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
<?php
|
2 |
-
|
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
|
7 |
-
Author URI: http://www.samuelaguilera.com
|
8 |
-
License: GPLv3
|
9 |
-
|
|
|
|
|
10 |
|
11 |
/*
|
12 |
This program is free software: you can redistribute it and/or modify
|
@@ -27,37 +29,38 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|
27 |
}
|
28 |
|
29 |
if ( ! defined( 'ATTACHMENT_REDIRECT_CODE' ) ) {
|
30 |
-
define
|
31 |
}
|
32 |
|
33 |
if ( ! defined( 'ORPHAN_ATTACHMENT_REDIRECT_CODE' ) ) {
|
34 |
-
define
|
35 |
}
|
36 |
|
37 |
|
|
|
|
|
|
|
38 |
function sar_attachment_redirect() {
|
39 |
|
40 |
global $post;
|
41 |
|
42 |
-
if ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && ( $post->post_parent
|
43 |
|
44 |
-
$parent_post_in_trash = get_post_status( $post->post_parent ) === 'trash' ? true : false;
|
45 |
|
46 |
-
if ( $parent_post_in_trash ) {
|
47 |
-
|
48 |
}
|
49 |
|
50 |
-
wp_safe_redirect( get_permalink( $post->post_parent ), ATTACHMENT_REDIRECT_CODE ); // Redirect to post/page from where attachment was uploaded
|
51 |
-
exit;
|
52 |
|
53 |
} elseif ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && ( $post->post_parent < 1 ) ) {
|
54 |
|
55 |
-
wp_safe_redirect( get_bloginfo( 'wpurl' ), ORPHAN_ATTACHMENT_REDIRECT_CODE ); // Redirect to home for attachments not associated to any post/page
|
56 |
exit;
|
57 |
|
58 |
}
|
59 |
-
|
60 |
-
|
61 |
}
|
62 |
|
63 |
-
add_action( 'template_redirect', 'sar_attachment_redirect', 1 );
|
1 |
<?php
|
2 |
+
/**
|
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 |
+
*
|
10 |
+
* @package Attachment Pages Redirect
|
11 |
+
*/
|
12 |
|
13 |
/*
|
14 |
This program is free software: you can redistribute it and/or modify
|
29 |
}
|
30 |
|
31 |
if ( ! defined( 'ATTACHMENT_REDIRECT_CODE' ) ) {
|
32 |
+
define( 'ATTACHMENT_REDIRECT_CODE', '301' ); // Default redirect code for attachments with existing parent post.
|
33 |
}
|
34 |
|
35 |
if ( ! defined( 'ORPHAN_ATTACHMENT_REDIRECT_CODE' ) ) {
|
36 |
+
define( 'ORPHAN_ATTACHMENT_REDIRECT_CODE', '302' ); // Default redirect code for attachments with no parent post.
|
37 |
}
|
38 |
|
39 |
|
40 |
+
/**
|
41 |
+
* Handles redirection.
|
42 |
+
*/
|
43 |
function sar_attachment_redirect() {
|
44 |
|
45 |
global $post;
|
46 |
|
47 |
+
if ( is_attachment() && isset( $post->post_parent ) && is_numeric( $post->post_parent ) && ( 0 !== $post->post_parent ) ) {
|
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 |
}
|
|
|
|
|
64 |
}
|
65 |
|
66 |
+
add_action( 'template_redirect', 'sar_attachment_redirect', 1 );
|
readme.txt
CHANGED
@@ -2,9 +2,9 @@
|
|
2 |
Contributors: samuelaguilera
|
3 |
Tags: attachment, redirect, images, 301, 302
|
4 |
Requires at least: 4.8
|
5 |
-
Requires PHP: 5.6
|
6 |
-
Tested up to:
|
7 |
-
Stable tag: 1.1
|
8 |
License: GPLv3
|
9 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
10 |
|
@@ -50,6 +50,10 @@ define( 'ORPHAN_ATTACHMENT_REDIRECT_CODE', '301' );
|
|
50 |
|
51 |
== Changelog ==
|
52 |
|
|
|
|
|
|
|
|
|
53 |
= 1.1 =
|
54 |
|
55 |
* Minor coding standards improvements.
|
2 |
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
|
10 |
|
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.
|
56 |
+
|
57 |
= 1.1 =
|
58 |
|
59 |
* Minor coding standards improvements.
|