Home / Widgets / Convert headings to sentence case
Duplicate Snippet

Embed Snippet on Your Site

Convert headings to sentence case

If you like your headings in sentence case this works. No changes WP Database: if snippet deactivated then text reverts to original form.

Dual Protection System:

1. Automatic Exceptions (No tags needed)
Words in the exceptions array are automatically protected:
- `'THIS API GUIDE'` → `'This API guide'` (API automatically stays caps)
- `'WORDPRESS TUTORIAL'` → `'WordPress tutorial'` (WordPress keeps proper case)

2. Manual Tags (For specific cases)
Use `[keep]` tags for one-off protections or words not in your exceptions list:
- `'CONTACT [kp]ABC Corp[/kp] TODAY'` → `'Contact ABC Corp today'`
- `'NEW [kp]iPhone 15[/kp] REVIEW'` → `'New iPhone 15 review'`

When to Use Each:

Exceptions Array: For words you use frequently across your site
- Technical terms: API, HTML, CSS, PHP
- Company names: WordPress, MySQL, AWS
- Titles: CEO, CTO, CFO

Tags: For occasional or unique cases
- Specific brand names not in your list
- Product names with special capitalization
- Unique proper nouns

Adding to Exceptions Array:
Just edit this line in the code to add your commonly used words:
```php
$exceptions = array(
'API', 'URL', 'HTML', 'CSS', 'PHP', 'SQL', 'XML', 'JSON', 'HTTP', 'HTTPS',
'CEO', 'CTO', 'CFO', 'USA', 'UK', 'EU', 'AI', 'ML', 'IoT', 'GPS',
'FAQ', 'PDF', 'SEO', 'ROI', 'KPI', 'B2B', 'B2C', 'SaaS',
'WordPress', 'WooCommerce', 'jQuery', 'JavaScript', 'MySQL', 'AWS',
'YOUR_WORD_HERE', 'ANOTHER_EXCEPTION'
);
```

This gives you the best of both worlds - automatic protection for common terms and manual control for special cases!

Code Preview
php
<?php
function convert_to_sentence_case($text) {
$exceptions = array(
'API', 'URL', 'HTML', 'CSS', 'PHP', 'SQL', 'XML', 'JSON', 'HTTP', 'HTTPS',
'CEO', 'CTO', 'CFO', 'USA', 'UK', 'EU', 'AI', 'ML', 'IoT', 'GPS',
'FAQ', 'PDF', 'SEO', 'ROI', 'KPI', 'B2B', 'B2C', 'SaaS',
'WordPress', 'WooCommerce', 'jQuery', 'JavaScript', 'MySQL', 'AWS'
);
$text = trim($text);
$protected_words = array();
$placeholder_counter = 0;
$text = preg_replace_callback('/\[kp\](.*?)\[\/kp\]/i', function($matches) use (&$protected_words, &$placeholder_counter) {
$placeholder = '___PROTECTED_' . $placeholder_counter . '___';
$protected_words[$placeholder] = $matches[1];
$placeholder_counter++;
return $placeholder;
}, $text);
$words = explode(' ', $text);
$processed_words = array();
foreach ($words as $index => $word) {
if (strpos($word, '___PROTECTED_') !== false) {
$processed_words[] = $word;
} else {
$clean_word = preg_replace('/[^\w]/', '', $word);
$punctuation = preg_replace('/[\w]/', '', $word);
if (in_array(strtoupper($clean_word), array_map('strtoupper', $exceptions))) {
$found_exception = '';
foreach ($exceptions as $exception) {
if (strtoupper($clean_word) === strtoupper($exception)) {
$found_exception = $exception;
break;
}
}
$processed_words[] = $found_exception . $punctuation;
} else {
$lower_word = strtolower($word);
if ($index === 0) {
$processed_words[] = ucfirst($lower_word);
} else {
$prev_word = isset($processed_words[$index - 1]) ? $processed_words[$index - 1] : '';
if (preg_match('/[.!?:]$/', $prev_word)) {
$processed_words[] = ucfirst($lower_word);
} else {
$processed_words[] = $lower_word;
}
}
}
}
}
$result = implode(' ', $processed_words);
foreach ($protected_words as $placeholder => $original) {
$result = str_replace($placeholder, $original, $result);
}
return $result;
}
function modify_heading_content($content) {
$pattern = '/<(h[1-6])([^>]*)>(.*?)<\/\1>/i';
$content = preg_replace_callback($pattern, function($matches) {
$tag = $matches[1];
$attributes = $matches[2];
$heading_content = $matches[3];
if (strpos($heading_content, '<a') !== false) {
$heading_content = preg_replace_callback('/<a([^>]*)>(.*?)<\/a>/i', function($link_matches) {
$link_attributes = $link_matches[1];
$link_text = $link_matches[2];
$converted_text = convert_to_sentence_case(strip_tags($link_text));
return '<a' . $link_attributes . '>' . $converted_text . '</a>';
}, $heading_content);
} else {
$clean_text = strip_tags($heading_content);
$heading_content = convert_to_sentence_case($clean_text);
}
return '<' . $tag . $attributes . '>' . $heading_content . '</' . $tag . '>';
}, $content);
return $content;
}
function modify_text_before_colons($content) {
$pattern = '/(?<![<\/])\b([A-Za-z][^:<>]*?)(\s*:)/';
$content = preg_replace_callback($pattern, function($matches) {
$text_before_colon = trim($matches[1]);
$colon_part = $matches[2];
$sentence_case_text = convert_to_sentence_case($text_before_colon);
return $sentence_case_text . $colon_part;
}, $content);
return $content;
}
add_filter('the_content', 'modify_heading_content', 20);
add_filter('the_content', 'modify_text_before_colons', 25);
add_filter('widget_text', 'modify_heading_content', 20);
add_filter('widget_text', 'modify_text_before_colons', 25);
add_filter('the_excerpt', 'modify_heading_content', 20);
add_filter('the_excerpt', 'modify_text_before_colons', 25);

Comments

Add a Comment