Last active
October 10, 2025 16:42
-
-
Save Vondelphia/b1b8937a6943e1016fa4a45bf25c3992 to your computer and use it in GitHub Desktop.
Replace all instances of a specific word and match original case
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
| // Replace all instances of "car" with "semi" across entire website (case-preserving) | |
| function replace_car_with_semi($content) { | |
| return preg_replace_callback('/\bcar\b/i', function($match) { | |
| // Preserve original case pattern | |
| if (ctype_upper($match[0])) { | |
| return 'SEMI'; // CAR -> SEMI | |
| } elseif (ctype_upper($match[0][0])) { | |
| return 'Semi'; // Car -> Semi | |
| } | |
| return 'semi'; // car -> semi | |
| }, $content); | |
| } | |
| // Apply to all major content areas | |
| add_filter('the_content', 'replace_car_with_semi'); // Post content | |
| add_filter('the_title', 'replace_car_with_semi'); // Post titles | |
| add_filter('the_excerpt', 'replace_car_with_semi'); // Excerpts | |
| add_filter('widget_text', 'replace_car_with_semi'); // Text widgets | |
| add_filter('widget_title', 'replace_car_with_semi'); // Widget titles | |
| add_filter('bloginfo', 'replace_car_with_semi'); // Site title/tagline | |
| add_filter('wp_nav_menu_items', 'replace_car_with_semi'); // Menu items | |
| add_filter('term_name', 'replace_car_with_semi'); // Category/tag names |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment