From bc5f6d91702c337400661dece148c86846a56fb4 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Tue, 25 May 2021 23:42:15 +0000 Subject: [PATCH] Formatting: Introduce the `document_title` filter. In `wp_get_document_title(), the returned value is currently passed directly through `wptexturize()`, `convert_chars()`, and `capital_P_dangit()`, and is done so after the `document_title_parts` filter is run. This makes it impossible to fully control the output of `wp_get_document_title()` and is inconsistent with how other similar text is processed with these functions. This commit introduces the `document_title` filter, which is run immediately before returning the results of the `wp_get_document_title()` function and moves the three formatting functions mentioned above to the new filter hook. This allows developers to further modify the title after being prepared by WordPress, or to modify the functions hooked to this filter as they wish. Props dragunoff, jeremyfelt, paaggeli, audrasjb. Fixes #51643. git-svn-id: https://develop.svn.wordpress.org/trunk@51019 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 4 ++-- src/wp-includes/general-template.php | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 535ed7261c..2dee4e2639 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -131,14 +131,14 @@ foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pr add_action( 'init', 'wp_init_targeted_link_rel_filters' ); // Format strings for display. -foreach ( array( 'comment_author', 'term_name', 'link_name', 'link_description', 'link_notes', 'bloginfo', 'wp_title', 'widget_title' ) as $filter ) { +foreach ( array( 'comment_author', 'term_name', 'link_name', 'link_description', 'link_notes', 'bloginfo', 'wp_title', 'document_title', 'widget_title' ) as $filter ) { add_filter( $filter, 'wptexturize' ); add_filter( $filter, 'convert_chars' ); add_filter( $filter, 'esc_html' ); } // Format WordPress. -foreach ( array( 'the_content', 'the_title', 'wp_title' ) as $filter ) { +foreach ( array( 'the_content', 'the_title', 'wp_title', 'document_title' ) as $filter ) { add_filter( $filter, 'capital_P_dangit', 11 ); } add_filter( 'comment_text', 'capital_P_dangit', 31 ); diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 6b67cd9a71..0b59b935d3 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -1238,10 +1238,15 @@ function wp_get_document_title() { $title = apply_filters( 'document_title_parts', $title ); $title = implode( " $sep ", array_filter( $title ) ); - $title = wptexturize( $title ); - $title = convert_chars( $title ); - $title = esc_html( $title ); - $title = capital_P_dangit( $title ); + + /** + * Filters the document title. + * + * @since 5.8.0 + * + * @param string $title Document title. + */ + $title = apply_filters( 'document_title', $title ); return $title; }