[wp-trac] [WordPress Trac] #58285: New filter hook for filtering whole HTML before final output
WordPress Trac
noreply at wordpress.org
Wed May 10 12:17:31 UTC 2023
#58285: New filter hook for filtering whole HTML before final output
-----------------------------+-----------------------------
Reporter: kubiq | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 6.2
Severity: normal | Keywords:
Focuses: performance |
-----------------------------+-----------------------------
Let's say I have 1 cache plugin that is great in cache but has no or bad
CSS and JS combining and minification options, so I have another plugin to
do that... than I have another plugin that is removing some unnecessary
styles and scripts from HTML on some pages and then I have another one
that fixes some width, height, loading, alt attributes...
These plugins use their own {{{ob_starts}}} again and again, flushing,
etc... some of them use some preg_match and replaces, some use domdocument
parser and then the website is very slow for administrator or on first
load before cache are created.
Currently, most cache, optimizing, assets plugins do something like this
{{{#!php
<?php
add_action( 'init', function(){
ob_start( 'my_magic_function' );
}, -1 );
function my_magic_function( $html ){
// preg_replaces
// maybe domdocument parsing
return $html;
}
}}}
and this is done again and again by every such-a-plugin
That's why I think it should be part of the core and I imagine something
like this:
{{{#!php
<?php
add_action( 'init', function(){
if( apply_filters( 'wp_enable_final_html_filtering', false ) ){
ob_start( 'default_wp_ob_handler' );
}
}, -1 );
function default_wp_ob_handler( $html ){
$dom = false;
if( apply_filters( 'wp_enable_final_html_dom', false ) ){
$dom = new DOMDocument();
$dom->loadHTML( $html );
$dom = apply_filters( 'wp_filter_final_html_dom', $dom );
$html = $dom->saveHTML();
}
return apply_filters( 'wp_filter_final_html', $html );
}
}}}
so then plugin developers can easily access html or DOM and change it
without the need to use ob_starts and flushes and parsers again and again,
eg.:
{{{#!php
<?php
add_filter( 'wp_enable_final_html_filtering', '__return_true' );
add_filter( 'wp_filter_final_html', function( $html ){
// preg_mathces and replacements
return $html;
});
// or if you need work with DOM structure
add_filter( 'wp_enable_final_html_filtering', '__return_true' );
add_filter( 'wp_filter_final_html_dom', function( $dom ){
// xpath searches and replacements
return $dom;
}, 10, 2 );
}}}
This can improve loading performance for admin users, or on the first load
of page before cache is created, it will be more consistent and all cache
and optimizing plugins can use these hooks easily then.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58285>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list