Last active
June 28, 2021 22:46
-
-
Save marklchaves/b6fbe195d2d0a697ddba12d3927075d1 to your computer and use it in GitHub Desktop.
WordPress Filter for Mutating Title Tags
This file contains 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 | |
/** | |
* 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