14

I am helping my father with his WordPress website.
It has over 1,700 posts with TITLES IN UPPERCASE.

We'd like to change these to "Title Case" in the database (possibly using this PHP script).

The WordPress "To Title Case" plug-in changes the case at the template level - we'd like to change it at the database level.

What would be the best way to apply the script to all titles in the WordPress database?
I could write some code from scratch but I'm guessing there's existing code/methods out there that can apply a function/method across all titles.

5
  • 1
    I would write a Loop form scratch. It should be pretty simple, but if you want to reuse the plugin's code then post the relevant parts.
    – s_ha_dum
    Commented Apr 6, 2013 at 23:27
  • I assume the titles where added manually in uppercase? Commented Jul 31, 2014 at 14:54
  • @BradDalton - that is correct, he got into a habit of typing in his article/ blog titles in UPPERCASE. Commented Aug 3, 2014 at 20:24
  • Have you found a solution for this yet? Commented Aug 3, 2014 at 23:28
  • @BradDalton - yes, the solution I selected below "Updating the posts" Commented Aug 4, 2014 at 1:41

5 Answers 5

20

Updating the posts

$all_posts = get_posts(
    'posts_per_page' => -1,
    'post_type' => 'post'
);

foreach ( $all_posts as $single ) {
    wp_update_post( array(
        'ID' => $single->ID,
        'post_title' => to_title_case( $single->post_title ) // see function below
    ));
}

Converting a string to "Title Case"

And, while not pertinent to WP, for the sake of completeness:

function to_title_case( $string ) {
     /* Words that should be entirely lower-case */
     $articles_conjunctions_prepositions = array(
          'a','an','the',
          'and','but','or','nor',
          'if','then','else','when',
          'at','by','from','for','in',
          'off','on','out','over','to','into','with'
     );
     /* Words that should be entirely upper-case (need to be lower-case in this list!) */
     $acronyms_and_such = array(
         'asap', 'unhcr', 'wpse', 'wtf'
     );
     /* split title string into array of words */
     $words = explode( ' ', mb_strtolower( $string ) );
     /* iterate over words */
     foreach ( $words as $position => $word ) {
         /* re-capitalize acronyms */
         if( in_array( $word, $acronyms_and_such ) ) {
             $words[$position] = mb_strtoupper( $word );
         /* capitalize first letter of all other words, if... */
         } elseif (
             /* ...first word of the title string... */
             0 === $position ||
             /* ...or not in above lower-case list*/
             ! in_array( $word, $articles_conjunctions_prepositions ) 
         ) {
             $words[$position] = ucwords( $word );
         }
     }         
     /* re-combine word array */
     $string = implode( ' ', $words );
     /* return title string in title case */
     return $string;
}

Obviously, both lists of words could be expanded - the lower-case list especially by more prepositions, the acronyms by those that are used often on the current site.

The WP-specific part is only the upper code block though, anyhow.

3
  • 1
    A title with WordPress, MySQL and OpenOffice :D
    – fuxia
    Commented Apr 27, 2013 at 1:04
  • :D Only saw this 2day. Yep, that's a gotcha... Commented Jul 31, 2014 at 15:07
  • How does the "Updating the posts" part fit in my template file in place of <?php the_title(); >?
    – Pete
    Commented Jun 19, 2017 at 14:09
10

You could change the post title when it is viewed:

add_action( 'the_post', 'wpse_94856_title_update' );

function wpse_94856_title_update( $post )
{
    if ( empty ( $post->post_title ) )
        return;

    $new_title = mb_convert_case( $post->post_title, MB_CASE_TITLE, "UTF-8" );

    if ( $post->post_title === $new_title )
        return;

    wp_update_post(
        array (
            'ID'         => $post->ID,
            'post_title' => $new_title
        )
    );

    // $post is passed by reference, so we update this property in real time
    $post->post_title = $new_title;
}

This is just an idea, based on this answer. Not tested.

1
  • Works well for me.
    – Pete
    Commented Jun 19, 2017 at 14:19
0

A quick "solution" would be via CSS using text-transform.

text-transform: capitalize;

However, it would be best if you could change the capitalization on the database since this is a matter of styles, not content :) If you want titles in uppercase, do so through CSS or you'll have this kind of problem!

1
  • 1
    This can only capitalise lowercase words, not words that are already capitalised.
    – Pete
    Commented Jun 19, 2017 at 14:02
0

This works on an individual title by title reference basis

 <?php print  ucwords(strtolower(get_the_title())); ?>

strtolower turns the title into lowercase. Then the ucwords makes it proper case

0

I created a Open Source, Free (no-Pro) plugin that offers exceptions to the rules and other things. The reason I made this was to use on Imports and situations like yours. It changes the Title in the Admin section and in the database, meaning it will work with all themes that are not using a text-transformation on the title. It will do Upper, lower and mixed. It doesn't currently do sentence case. Please see my ProperProgramming link to see screenshots of how it works. Super easy and you can do it in chunks. The plugin also has configurable exceptions for lower and UPPER case words. So it will do Acronyms.

https://properprogramming.com/tools/wp-change-titles-case/

Or directly on WordPress

https://wordpress.org/plugins/change-titles-case/

2
  • Please edit your answer to provide more detail as to why your plugin is a good solution to the problem. As it stands, it looks closer to self-promo than a genuine answer -- see Promotion for more info.
    – Pat J
    Commented Sep 28, 2020 at 16:19
  • Thanks, its edited. The plugin provides a database level transform that handles exceptions. This allows for conjunctions and acronyms to be configured. I made it public for promotion, but its a FREE tool with no Pro. Commented Sep 28, 2020 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.