feat: add filter for old slug redirect status and update tests#9862
feat: add filter for old slug redirect status and update tests#9862kushagra-goyal-14 wants to merge 5 commits intoWordPress:trunkfrom
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
src/wp-includes/query.php
Outdated
| /** | ||
| * Filters the old slug redirect status code. | ||
| * | ||
| * @since 6.8.0 |
There was a problem hiding this comment.
Should be 6.9.0
| * @since 6.8.0 | |
| * @since 6.9.0 |
Overview
This PR introduces a new filter
old_slug_redirect_statusthat allows developers to customize the HTTP redirect status code used by thewp_old_slug_redirect()function. Currently, the redirect status is hardcoded as 301 (permanent redirect), but this change enables the use of 302 (temporary redirect) or other status codes as needed.Background
WordPress automatically redirects old post URLs to new ones when a post's slug changes, helping maintain SEO value and user experience. However, the redirect status code was hardcoded as 301, limiting flexibility for developers who might need different redirect behaviors.
Fixes: #52737
Changes Made
Core Changes (
src/wp-includes/query.php)old_slug_redirect_statusin thewp_old_slug_redirect()function$status(int): The HTTP response status code (default: 301)$id(int): The post ID being redirected to@since 6.8.0tagTest Coverage (
tests/phpunit/tests/rewrite/oldSlugRedirect.php)test_old_slug_redirect_status_filter()Usage Examples
Change redirect to temporary (302)
Conditional redirect based on post type
Disable redirect for specific posts
Use different status codes based on post age
Filter Reference
old_slug_redirect_statusDescription: Filters the HTTP status code used for old slug redirects.
Parameters:
$status(int): The HTTP response status code to use for the redirect. Default 301.$id(int): The post ID being redirected to.Return Value:
Example: