-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move redirects from old theme to plugin
This means they work with both old and new theme activated See #2496
- Loading branch information
1 parent
19cca2e
commit fbafe66
Showing
3 changed files
with
66 additions
and
61 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace WPOrg_Learn\Redirects; | ||
|
||
add_action( 'template_redirect', __NAMESPACE__ . '\wporg_learn_redirect_meetings' ); | ||
add_action( 'template_redirect', __NAMESPACE__ . '\wporg_learn_redirect_old_urls' ); | ||
|
||
/** | ||
* Redirect meeting posts to associated link | ||
* | ||
* @return void | ||
*/ | ||
function wporg_learn_redirect_meetings() { | ||
global $post; | ||
|
||
if ( is_singular( array( 'meeting' ) ) ) { | ||
|
||
if ( ! empty( $post->ID ) ) { | ||
|
||
$redirect = wp_http_validate_url( get_post_meta( $post->ID, 'link', true ) ); | ||
|
||
if ( $redirect && wp_redirect( $redirect ) ) { | ||
exit; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Redirect old pages to their new homes. | ||
* | ||
* @return void | ||
*/ | ||
function wporg_learn_redirect_old_urls() { | ||
if ( ! is_404() ) { | ||
return; | ||
} | ||
|
||
$redirects = array( | ||
// Source => Destination, any characters after the source will be appended to the destination. | ||
'/workshop/' => '/tutorial/', | ||
'/workshops' => '/tutorials', | ||
'/social-learning' => '/online-workshops', | ||
'/workshop-presenter-application' => '/tutorial-presenter-application', | ||
'/report-content-errors' => '/report-content-feedback', | ||
); | ||
|
||
// Use `REQUEST_URI` rather than `$wp->request`, to get the entire source URI including url parameters. | ||
$request = $_SERVER['REQUEST_URI'] ?? ''; | ||
|
||
foreach ( $redirects as $source => $destination ) { | ||
if ( str_starts_with( $request, $source ) ) { | ||
$redirect = $destination; | ||
|
||
// Append any extra request parameters. | ||
if ( strlen( $request ) > strlen( $source ) ) { | ||
$redirect .= substr( $request, strlen( $source ) ); | ||
} | ||
|
||
wp_safe_redirect( $redirect ); | ||
die(); | ||
} | ||
} | ||
} |
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
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