Skip to content

Instantly share code, notes, and snippets.

@seothemes
Last active October 14, 2022 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seothemes/6975cbeb0a3c9188962f1f9994c58e82 to your computer and use it in GitHub Desktop.
Save seothemes/6975cbeb0a3c9188962f1f9994c58e82 to your computer and use it in GitHub Desktop.
Randomize text content in heading block
<?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