search

Home  >  Q&A  >  body text

Enhance WordPress by capitalizing post and page titles (h1 – h6)

I want to create a function that will capitalize (the first letter of each word is capitalized) in the title of my WordPress site.

I have been able to develop a regular expression for filtering preg_match_all().

The question is how to iterate the matches and capitalize the title using the ucwords() function. Finally, insert the uppercase title into the content.

I tried this code. The question is how to proceed after if (is_array())

function headings_in_the_content($content)
{
    $regexpattern = '#(?P<full_tag><(?P<tag_name>h\d)(?P<tag_extra>[^>]*)>(?P<tag_contents>[^<]*)</h\d>)#i';
    if (preg_match_all($regexpattern, $content, $matches)) {
        foreach ($matches as $regexmatches) {
            
             if (is_array($regexmatches)) {
                    foreach ($regexmatches as $regexmatch)
                        {

                        }
            }

        }
        
    }

    return $content;
}

add_filter('the_content', 'headings_in_the_content', 15000);

P粉023326773P粉023326773493 days ago573

reply all(1)I'll reply

  • P粉600845163

    P粉6008451632023-09-10 00:47:46

    If it was just for styling purposes, I would use css text-transform instead.

    https://developer.mozilla.org/en -US/docs/Web/CSS/text-transform

    reply
    0
  • Cancelreply