If the code can’t find body, it erases part of the page
-
Here is the body tag from my template:
<body class="home page-template-default page page-id-9 logged-in admin-bar no-customize-support wp-custom-logo wp-embed-responsive fl-builder no-sidebar nav-float-right separate-containers fluid-header active-footer-widgets-4 header-aligned-left dropdown-hover full-width-content" itemtype="https://schema.org/WebPage" itemscope>
Here is the plugin code:
add_action( 'wp_footer', function() use($yydev_tag_body_tag, $yydev_tagmanager_settings) { if( yydev_tagmanager_exclude_pages_check($yydev_tagmanager_settings) ) { $yydev_buffer = ob_get_clean(); $yydev_str_replace ='/<[bB][oO][dD][yY]\s[A-Za-z]{2,5}[A-Za-z0-9 "_=\-\.]+>|<body>/'; ob_start(); if( preg_match($yydev_str_replace, $yydev_buffer, $yydev_buffer_return) ) { $yydev_new_code = $yydev_buffer_return[0] . $yydev_tag_body_tag; $yydev_new_code = preg_replace($yydev_str_replace, $yydev_new_code, $yydev_buffer); echo $yydev_new_code; } // if( preg_match($yydev_str_replace, $yydev_buffer, $yydev_buffer_return) ) { ob_flush(); } // if( yydev_tagmanager_exclude_pages_check($yydev_tagmanager_settings) ) {
The regexp does not match the tag because it does not contain / and :
Moreover, if preg_match returns false, the code will lost part of content previously stored in yydev_buffer
So the correct code should be
add_action( 'wp_footer', function() use($yydev_tag_body_tag, $yydev_tagmanager_settings) { if( yydev_tagmanager_exclude_pages_check($yydev_tagmanager_settings) ) { $yydev_buffer = ob_get_clean(); $yydev_str_replace ='/<[bB][oO][dD][yY]\s[A-Za-z]{2,5}[A-Za-z0-9 "_=\-\.\/:]+>|<body>/'; ob_start(); if( preg_match($yydev_str_replace, $yydev_buffer, $yydev_buffer_return) ) { $yydev_new_code = $yydev_buffer_return[0] . $yydev_tag_body_tag; $yydev_new_code = preg_replace($yydev_str_replace, $yydev_new_code, $yydev_buffer); echo $yydev_new_code; } // if( preg_match($yydev_str_replace, $yydev_buffer, $yydev_buffer_return) ) { else { echo $yydev_buffer; } ob_flush(); } // if( yydev_tagmanager_exclude_pages_check($yydev_tagmanager_settings) ) {
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘If the code can’t find body, it erases part of the page’ is closed to new replies.