views:

83

answers:

5

So what i need is a query or some tip on how to turn all titles on a wordpress powered website into capitalized case.

What i have now is something like this:

AAAAA BBBBB CCCCC

I want it to be like this:

Aaaaa Bbbbb Ccccc

I did try googling and searching here, but have failed at that task so any help is much appreciated!

UPDATE:

I need to update titles inside the database. Just to be clear. :)

A: 

could you try wrapping wordpresses the_title(); function with ucwords and strtolower

<?php echo ucwords(strtolower(the_title(null, null, false))); ?>

From what i can gather this takes the title value uses strtolower to turn it into lowercase, then ucwords to capitalize each word.

I haven't tried this myself so i don't know it it works but this is how i would try it.

Hope this helps

EDIT: right i've had a look at one of my old files, in your functions.php you could define a function to hook into the save_post action. Using the post variable you should be able to adjust that data, but like others have said you have to be careful incase it doesnt produce the desired effect.

add_action('save_post', 'save_postdata');
function save_postdata($post_id) {
    //edit code here
    update_post_meta($post_id, 'title', $title);
}

I'm using the update_post_meta() function, i'm not entirely sure if this has the ability to edit the title, i don't have to ability to run a test unfortunately.

What do you guys think?

Rob
Hmm i guess that could work, but i what i want is to update data in database. This soulion would load AAAAA BBBBB CCCCC title from database and then turn it into Aaaaa Bbbbb Ccccc which would still leave that title looking like AAAAA BBBBB CCCCC in side my database. If i am not being clear with my question just let me know and i will try to explain a bit more.
GaVrA
mmm, yeah i know what you mean, well thats a bit more complex hold on ill see if i can find an example...
Rob
A: 

Just use ucwords( $title ) on their own - but check your use cases very carefully - acronyms do not end up displaying as your users might expect.

"beginners guide to TLAs" will become "Beginners Guide To Tlas"

Cups
+1  A: 

You could leave it as-is in the database and force the title to display with first-letters in capitals via css:

h2 {
  text-transform:capitalize;
}
McGirl
+2  A: 

There is no function in MySQL for this, but you can create one like this:

DROP FUNCTION IF EXISTS proper;
SET GLOBAL  log_bin_trust_function_creators=TRUE;
DELIMITER |
CREATE FUNCTION proper( str VARCHAR(128) )
RETURNS VARCHAR(128)
BEGIN
  DECLARE c CHAR(1);
  DECLARE s VARCHAR(128);
  DECLARE i INT DEFAULT 1;
  DECLARE BOOL INT DEFAULT 1;
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
  SET s = LCASE( str );
  WHILE i < LENGTH( str ) DO 
    BEGIN
      SET c = SUBSTRING( s, i, 1 );
      IF LOCATE( c, punct ) > 0 THEN
        SET BOOL = 1;
      ELSEIF BOOL=1 THEN 
        BEGIN
          IF c >= 'a' AND c <= 'z' THEN 
            BEGIN
              SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
              SET BOOL = 0;
            END;
          ELSEIF c >= '0' AND c <= '9' THEN
            SET BOOL = 0;
          END IF;
        END;
      END IF;
      SET i = i+1;
    END;
  END WHILE;
  RETURN s;
END;
|
DELIMITER ;

From here.

The you can update easily by running:

Update wp_posts
Set post_title = proper(post_title)
brendan
A: 

For the record, the MySQL function quoted above fails when the last character stands alone, e.g.,

Public John q

There's an easy workaround, if you don't need padding:

trim(proper(concat(myfield, '    ')))
thatnewguy