Last active
October 14, 2022 23:04
-
-
Save seothemes/6975cbeb0a3c9188962f1f9994c58e82 to your computer and use it in GitHub Desktop.
Randomize text content in heading block
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
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment