Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/wp-includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,10 +1108,12 @@ function wp_old_slug_redirect() {
* Filters the old slug redirect URL.
*
* @since 4.4.0
* @since x.x.0 Added the `$id` parameter.
*
* @param string $link The redirect URL.
* @param int $id The redirect post ID.
*/
$link = apply_filters( 'old_slug_redirect_url', $link );
$link = apply_filters( 'old_slug_redirect_url', $link, $id );

if ( ! $link ) {
return;
Expand Down
34 changes: 30 additions & 4 deletions tests/phpunit/tests/rewrite/oldSlugRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
protected $old_slug_redirect_url;

protected $old_slug_redirect_post_id;

protected static $post_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
Expand All @@ -22,7 +24,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
public function set_up() {
parent::set_up();

add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 );
add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 2 );

$this->set_permalink_structure( '/%postname%/' );

Expand All @@ -33,7 +35,8 @@ public function set_up() {
}

public function tear_down() {
$this->old_slug_redirect_url = null;
$this->old_slug_redirect_url = null;
$this->old_slug_redirect_post_id = null;

parent::tear_down();
}
Expand Down Expand Up @@ -181,6 +184,28 @@ public function test_old_slug_redirect_paged() {
$this->assertSame( $permalink, $this->old_slug_redirect_url );
}

/**
* @ticket 39442
*/
public function test_old_slug_redirect_url_filter_receives_redirect_post_id() {
$old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );

wp_update_post(
array(
'ID' => self::$post_id,
'post_name' => 'bar-baz',
)
);

$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertSame(
self::$post_id,
$this->old_slug_redirect_post_id,
'The old slug redirect URL filter should receive the redirect post ID.'
);
}

/**
* @ticket 35031
*/
Expand Down Expand Up @@ -210,8 +235,9 @@ public function test_old_slug_doesnt_redirect_when_reused() {
$this->assertNull( $this->old_slug_redirect_url );
}

public function filter_old_slug_redirect_url( $url ) {
$this->old_slug_redirect_url = $url;
public function filter_old_slug_redirect_url( $url, $post_id ) {
$this->old_slug_redirect_url = $url;
$this->old_slug_redirect_post_id = $post_id;
return false;
}
}
Loading