mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-09 23:24:34 +00:00
Media: Optimize images created in shortcodes.
This fixes an issue where images dynamically created during shortcode rendering (e.g., shortcode image galleries), were not getting image optimizations like `loading="lazy"` or `fetchpriority="hight"` applied. Note that even after this commit, shortcodes are processed after the main content images, which can affect the order in which optimizations are applied in content areas. Follow-up to [56037]. Props spacedmonkey, flixos90, thekt12, swissspidy, joemcgill. Fixes #58681. git-svn-id: https://develop.svn.wordpress.org/trunk@56214 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -221,6 +221,14 @@ function do_shortcode( $content, $ignore_html = false ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// Ensure this context is only added once if shortcodes are nested.
|
||||
$has_filter = has_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' );
|
||||
$filter_added = false;
|
||||
|
||||
if ( ! $has_filter ) {
|
||||
$filter_added = add_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' );
|
||||
}
|
||||
|
||||
$content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames );
|
||||
|
||||
$pattern = get_shortcode_regex( $tagnames );
|
||||
@@ -229,9 +237,29 @@ function do_shortcode( $content, $ignore_html = false ) {
|
||||
// Always restore square braces so we don't break things like <!--[if IE ]>.
|
||||
$content = unescape_invalid_shortcodes( $content );
|
||||
|
||||
// Only remove the filter if it was added in this scope.
|
||||
if ( $filter_added ) {
|
||||
remove_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the `wp_get_attachment_image_context` hook during shortcode rendering.
|
||||
*
|
||||
* When wp_get_attachment_image() is called during shortcode rendering, we need to make clear
|
||||
* that the context is a shortcode and not part of the theme's template rendering logic.
|
||||
*
|
||||
* @since 6.3.0
|
||||
* @access private
|
||||
*
|
||||
* @return string The filtered context value for wp_get_attachment_images when doing shortcodes.
|
||||
*/
|
||||
function _filter_do_shortcode_context() {
|
||||
return 'do_shortcode';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the shortcode regular expression for searching.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user