Skip to main content
deleted 6 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53
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', 'so', '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;
}
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', 'so', '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;
}
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;
}
added 774 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53

Updating the posts:

Updating the posts

Converting a string to "Title Case"

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', 'so', '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;
}

This title case conversion function certainly is not perfect - the listObviously, both lists of words tocould be left inexpanded - the lower-case could be expanded,list especially by more prepositions. Also it will mess with, the acronyms and other wordsby those that should remain in all-capsare used often on the current site.

Updating the posts:

function to_title_case( $string ) {
     $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 = explode( ' ', mb_strtolower( $string ) );

     foreach ( $words as $position => $word ) {
         if (
             0 === $position ||
             ! in_array( $word, $articles_conjunctions_prepositions )
         ) {
             $words[$position] = ucwords( $word );
         }
     }

     $string = implode( ' ', $words );

     return $string;
}

This title case conversion function certainly is not perfect - the list of words to be left in lower-case could be expanded, especially by more prepositions. Also it will mess with acronyms and other words that should remain in all-caps.

Updating the posts

Converting a string to "Title Case"

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', 'so', '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.

added 1 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53

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
    ));
}

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

function to_title_case( $string ) {
     $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 = explode( ' ', mb_strtolower( $string ) );

     foreach ( $words as $position => $word ) {
         if (
             0 === $position ||
             ! in_array( $word, $articles_conjunctions_prepositions )
         ) {
             $words[$position] = ucwords( $word );
         }
     }

     $string = implode( ' ', $words );

     return $string;
}

This title case conversion function certainly is not perfect - the list of words to be left in lower-case could be expanded, especially by more prepositions. Also it will mess with acronyms and other words that should remain in all-caps.

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

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
    ));
}

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

function to_title_case( $string ) {
     $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 = explode( ' ', mb_strtolower( $string ) );

     foreach ( $words as $position => $word ) {
         if (
             0 === $position ||
             ! in_array( $word, $articles_conjunctions_prepositions )
         ) {
             $words[$position] = ucwords( $word );
         }
     }

     $string = implode( ' ', $words );

     return $string;
}

This title case conversion function certainly is not perfect - the list of words to be left in lower-case could be expanded, especially by more prepositions. Also it will mess with acronyms and other words that should remain in all-caps.

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

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
    ));
}

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

function to_title_case( $string ) {
     $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 = explode( ' ', mb_strtolower( $string ) );

     foreach ( $words as $position => $word ) {
         if (
             0 === $position ||
             ! in_array( $word, $articles_conjunctions_prepositions )
         ) {
             $words[$position] = ucwords( $word );
         }
     }

     $string = implode( ' ', $words );

     return $string;
}

This title case conversion function certainly is not perfect - the list of words to be left in lower-case could be expanded, especially by more prepositions. Also it will mess with acronyms and other words that should remain in all-caps.

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

added 22 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53
Loading
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53
Loading