diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 2429ab3adb776..70974f7210811 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -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( '', - esc_attr( get_permalink( $post->ID ) ) + esc_attr( $redirect_url ) ); } diff --git a/tests/phpunit/tests/post/getThePasswordForm.php b/tests/phpunit/tests/post/getThePasswordForm.php new file mode 100644 index 0000000000000..d767647ea1171 --- /dev/null +++ b/tests/phpunit/tests/post/getThePasswordForm.php @@ -0,0 +1,27 @@ +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 ); + } +}