Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Last active June 28, 2021 22:46
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 marklchaves/b6fbe195d2d0a697ddba12d3927075d1 to your computer and use it in GitHub Desktop.
Save marklchaves/b6fbe195d2d0a697ddba12d3927075d1 to your computer and use it in GitHub Desktop.
WordPress Filter for Mutating Title Tags
<?php
/**
* This function mutates all H2s to become H3s. Add
* this to your child theme's functions.php file.
*
* Change or add to this as needed. I.e., change the
* target tag and replacement tag to whatever you want.
*
* Add more lines to remove more title tags, if you want.
*
* Reference
* - https://www.php.net/manual/en/function.preg-replace.php
*
* Examples
*
* Remove H2s Completely
* - Change to preg_replace('/<h2>/', '', $theContent) and preg_replace('/<\/h2>/', '', $theContent);
*
* Make all H2s Become divs
* - Change to preg_replace('/<h2>/', '<div>', $theContent) and preg_replace('/<\/h2>/', '</div>', $theContent);
*
* Change all heading tags to h3 while preserving any tag attributes.
* - $theContent = preg_replace('/<h([1-6])(.*?)<\/h([1-6])>/si', '<h3 $2</h3>', $theContent);
*
* Don't forget to change the name of the function to keep
* it meaningful.
*/
function h2_to_h3 ($content) {
$theContent = $content;
$theContent = preg_replace('/<h2>/', '<h3>', $theContent);
$theContent = preg_replace('/<\/h2>/', '</h3>', $theContent);
return $theContent;
}
add_filter('the_content', 'h2_to_h3');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment