Last active
July 3, 2019 22:01
-
-
Save WPprodigy/805b6cd76ffd95a9072786afdb79c707 to your computer and use it in GitHub Desktop.
Conditionally remove a SRM redirect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Remove the SRM redirect for 'doc_id' query strings after a certain ID is reached. | |
*/ | |
add_filter( 'srm_registered_redirects', function( $redirects, $requested_path ) { | |
// The last post ID that we want to be automatically redirected. | |
$redirect_breakpoint_id = 565000; | |
// The SRM post ID for the redirect we will conditionally remove. | |
$safe_redirect_post_id = 18; | |
// Check if the requested path has the doc_id query string, and if so, compare to the breakpoint ID. | |
$remove_srm_redirect = false; | |
if ( preg_match( '/\/.+doc_id=(\d+).{0,}/', $requested_path, $matches ) ) { | |
if ( isset( $matches[1] ) && (int) $matches[1] > $redirect_breakpoint_id ) { | |
$remove_srm_redirect = true; | |
} | |
} | |
// Remove the redirect if we're above the breakpoint ID. | |
if ( $remove_srm_redirect ) { | |
foreach ( $redirects as $redirect_key => $redirect ) { | |
if ( $redirect['ID'] === $safe_redirect_post_id ) { | |
unset( $redirects[ $redirect_key ] ); | |
} | |
} | |
} | |
return $redirects; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment