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
13 changes: 12 additions & 1 deletion src/wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1803,9 +1803,20 @@ function get_the_password_form( $post = 0 ) {
}

if ( ! empty( $post->ID ) ) {

/**
* Filters the redirect URL used in the post password form.
*
* @since 6.x.x
*
* @param string $redirect_url The redirect URL. Default is the post permalink.
* @param WP_Post $post The post object.
*/
$redirect_url = apply_filters( 'the_password_form_redirect_url', get_permalink( $post->ID ), $post );

$redirect_field = sprintf(
'<input type="hidden" name="redirect_to" value="%s" />',
esc_attr( get_permalink( $post->ID ) )
esc_attr( $redirect_url )
);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/tests/post/getThePasswordForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @group post
* @group template
*/
class Tests_Post_GetThePasswordForm extends WP_UnitTestCase {

/**
* Tests that the_password_form_redirect_url filter can modify the redirect destination.
*
* @ticket 64785
*/
public function test_the_password_form_redirect_url_filter() {
$post_id = self::factory()->post->create();
$custom_url = 'https://example.com/custom-preview-link/';

$callback = function () use ( $custom_url ) {
return $custom_url;
};

add_filter( 'the_password_form_redirect_url', $callback );

$form = get_the_password_form( $post_id );

$this->assertStringContainsString( 'value="' . $custom_url . '"', $form );
}
}
Loading