Skip to content

Instantly share code, notes, and snippets.

@Vondelphia
Last active October 10, 2025 16:42
Show Gist options
  • Select an option

  • Save Vondelphia/b1b8937a6943e1016fa4a45bf25c3992 to your computer and use it in GitHub Desktop.

Select an option

Save Vondelphia/b1b8937a6943e1016fa4a45bf25c3992 to your computer and use it in GitHub Desktop.
Replace all instances of a specific word and match original case
// 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