[wp-trac] [WordPress Trac] #58285: New filter hook for filtering whole HTML before final output

WordPress Trac noreply at wordpress.org
Wed May 10 21:46:59 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           |  Resolution:
 Keywords:                   |     Focuses:  performance
-----------------------------+------------------------------

Comment (by SergeyBiryukov):

 Hi there, welcome back to WordPress Trac! Thanks for the ticket.

 Just noting that this seems related to #43258.

 Replying to [ticket:58285 kubiq]:
 > {{{#!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 );
 > }
 > }}}

 In this example, the `wp_enable_*` filters appear to be redundant, I think
 we could use `has_filter()` instead:
 {{{#!php
 <?php
 add_action( 'init', function(){
         if( has_filter( 'wp_filter_final_html' ) || has_filter(
 'wp_filter_final_html_dom' ) ){
                 ob_start( 'default_wp_ob_handler' );
         }
 }, -1 );

 function default_wp_ob_handler( $html ){
         $dom = false;
         if( has_filter( 'wp_filter_final_html_dom' ) ){
                 $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 );
 }
 }}}

 That way only one hook would be needed to enable the filtering, instead of
 two.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58285#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list