Randomize content of H1 heading block on front page

Copied
<?php add_filter( 'render_block_core/heading', __NAMESPACE__ . '\\randomize_h1_heading_block_on_front_page', 10, 2 ); /** * Randomizes content of H1 heading blocks on the front page. * * @since 1.0.0 * * @param string $content Block HTML content. * @param array $block Block data. * * @return string */ function randomize_h1_heading_block_on_front_page( string $content, array $block ): string { $page_condition = \is_front_page(); $heading_level = $block['attrs']['level'] ?? null; if ( $page_condition && $heading_level === 1 ) { $strings = [ 'Random title one', 'Another random title', 'Third random title', ]; $content = \str_replace( \trim( \strip_tags( $content ) ), $strings[ \array_rand( $strings ) ], $content ); } return $content; }
Code language: HTML, XML (xml)