Skip to content

feat: add filter for old slug redirect status and update tests#9862

Open
kushagra-goyal-14 wants to merge 5 commits intoWordPress:trunkfrom
kushagra-goyal-14:feat/allow-301-redirect
Open

feat: add filter for old slug redirect status and update tests#9862
kushagra-goyal-14 wants to merge 5 commits intoWordPress:trunkfrom
kushagra-goyal-14:feat/allow-301-redirect

Conversation

@kushagra-goyal-14
Copy link
Copy Markdown

@kushagra-goyal-14 kushagra-goyal-14 commented Sep 12, 2025

Overview

This PR introduces a new filter old_slug_redirect_status that allows developers to customize the HTTP redirect status code used by the wp_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)

  1. Added new filter: old_slug_redirect_status in the wp_old_slug_redirect() function
  2. Filter parameters:
    • $status (int): The HTTP response status code (default: 301)
    • $id (int): The post ID being redirected to
  3. Validation: Added check to prevent redirect if filter returns falsy value
  4. Documentation: Added proper PHPDoc with @since 6.8.0 tag

Test Coverage (tests/phpunit/tests/rewrite/oldSlugRedirect.php)

  1. New test method: test_old_slug_redirect_status_filter()
  2. Test scenarios:
    • Default 301 redirect behavior (backward compatibility)
    • Custom 302 redirect status via filter
    • Redirect prevention by returning 0/false
    • Proper post ID parameter passing
  3. Helper methods: Added filter callbacks and redirect capture functionality

Usage Examples

Change redirect to temporary (302)

add_filter( 'old_slug_redirect_status', function( $status, $post_id ) {
    return 302; // Temporary redirect
}, 10, 2 );

Conditional redirect based on post type

add_filter( 'old_slug_redirect_status', function( $status, $post_id ) {
    $post = get_post( $post_id );
    if ( $post && $post->post_type === 'product' ) {
        return 302; // Temporary redirect for products
    }
    return $status; // Keep default 301 for other post types
}, 10, 2 );

Disable redirect for specific posts

add_filter( 'old_slug_redirect_status', function( $status, $post_id ) {
    $disabled_posts = [123, 456, 789];
    if ( in_array( $post_id, $disabled_posts ) ) {
        return false; // No redirect
    }
    return $status;
}, 10, 2 );

Use different status codes based on post age

add_filter( 'old_slug_redirect_status', function( $status, $post_id ) {
    $post = get_post( $post_id );
    if ( $post ) {
        $post_age_days = ( time() - strtotime( $post->post_date ) ) / DAY_IN_SECONDS;
        
        // Use 302 for recent posts (less than 30 days old)
        if ( $post_age_days < 30 ) {
            return 302;
        }
        
        // Use 301 for older posts (default behavior)
        return 301;
    }
    
    return $status;
}, 10, 2 );

Filter Reference

old_slug_redirect_status

Description: 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:

  • (int|false) The HTTP status code to use, or false to prevent the redirect.

Example:

/**
 * Change old slug redirects to use 302 status for draft posts.
 *
 * @param int $status The redirect status code.
 * @param int $id     The post ID being redirected to.
 * @return int|false  Modified status code or false to prevent redirect.
 */
function custom_old_slug_redirect_status( $status, $id ) {
    $post = get_post( $id );
    
    if ( $post && $post->post_status === 'draft' ) {
        return 302; // Temporary redirect for drafts
    }
    
    return $status; // Use default for published posts
}
add_filter( 'old_slug_redirect_status', 'custom_old_slug_redirect_status', 10, 2 );

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@kushagra-goyal-14 kushagra-goyal-14 marked this pull request as ready for review September 12, 2025 13:19
@github-actions
Copy link
Copy Markdown

github-actions bot commented Sep 12, 2025

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props kush123, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

/**
* Filters the old slug redirect status code.
*
* @since 6.8.0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 6.9.0

Suggested change
* @since 6.8.0
* @since 6.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants