Created
March 29, 2026 19:59
-
-
Save cre8tivediva/08f91b9084dc2fafadeaa89b0b36b647 to your computer and use it in GitHub Desktop.
Temporary Fix: Product Block Title block renders current page/post title on frontend when used in regular post content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Temporary fix for the Product Block Title so the Product Title will display instead of the Post Title. Bug report sent: https://github.com/woocommerce/woocommerce/issues/63799 | |
| add_action( 'template_redirect', function() { | |
| if ( is_admin() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { | |
| return; | |
| } | |
| if ( ! is_singular() ) { | |
| return; | |
| } | |
| ob_start( function( $html ) { | |
| if ( false === strpos( $html, 'wp-block-woocommerce-single-product' ) ) { | |
| return $html; | |
| } | |
| libxml_use_internal_errors( true ); | |
| $dom = new DOMDocument(); | |
| $loaded = $dom->loadHTML( | |
| mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ), | |
| LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | |
| ); | |
| if ( ! $loaded ) { | |
| return $html; | |
| } | |
| $xpath = new DOMXPath( $dom ); | |
| $single_products = $xpath->query( | |
| '//div[contains(concat(" ", normalize-space(@class), " "), " wp-block-woocommerce-single-product ")]' | |
| ); | |
| if ( ! $single_products || 0 === $single_products->length ) { | |
| return $html; | |
| } | |
| foreach ( $single_products as $single_product ) { | |
| if ( ! $single_product->hasAttribute( 'data-product-id' ) ) { | |
| continue; | |
| } | |
| $product_id = absint( $single_product->getAttribute( 'data-product-id' ) ); | |
| if ( ! $product_id || 'product' !== get_post_type( $product_id ) ) { | |
| continue; | |
| } | |
| $product_title = get_the_title( $product_id ); | |
| $product_link = get_permalink( $product_id ); | |
| if ( ! $product_title || ! $product_link ) { | |
| continue; | |
| } | |
| $title_nodes = $xpath->query( | |
| './/*[contains(concat(" ", normalize-space(@class), " "), " wp-block-post-title ")]', | |
| $single_product | |
| ); | |
| if ( ! $title_nodes || 0 === $title_nodes->length ) { | |
| continue; | |
| } | |
| $title_node = $title_nodes->item( 0 ); | |
| $link_nodes = $xpath->query( './/a', $title_node ); | |
| if ( $link_nodes && $link_nodes->length > 0 ) { | |
| $link_node = $link_nodes->item( 0 ); | |
| $link_node->setAttribute( 'href', $product_link ); | |
| while ( $link_node->firstChild ) { | |
| $link_node->removeChild( $link_node->firstChild ); | |
| } | |
| $link_node->appendChild( $dom->createTextNode( $product_title ) ); | |
| } else { | |
| while ( $title_node->firstChild ) { | |
| $title_node->removeChild( $title_node->firstChild ); | |
| } | |
| $new_link = $dom->createElement( 'a', $product_title ); | |
| $new_link->setAttribute( 'href', $product_link ); | |
| $new_link->setAttribute( 'target', '_self' ); | |
| $title_node->appendChild( $new_link ); | |
| } | |
| } | |
| return $dom->saveHTML(); | |
| } ); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment