Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to check the if the first letter of a field is capitalized.

Example:

If (First Name field) is capitalized {

Capitalized!

} else {

Not Capitalized!

}

Thanks

Thank you

share|improve this question
1  
not a specific wordpress question; possibly try the other suggestions, but with php.net/manual/en/function.ucfirst.php –  Michael Nov 9 '11 at 17:10
    
Ok will do! Thanks Michael –  user1893 Nov 9 '11 at 17:12
add comment

2 Answers

up vote 1 down vote accepted
$field = 'First Name';

if(ucwords($field) !== $field){
 // not capitalized

}else{
 // capitalized

}

see ucwords()

share|improve this answer
    
Thanks One Trick Pony, I'll try it out! –  user1893 Nov 9 '11 at 15:56
    
I ran your solution in a loop that goes through all the users, but the didn't want to use the else statement. And for some reason when I set $field = get_user_meta($user->ID, 'display_name', true); I get a list of all the users and this indicates that the field is not capitalized. It should only show the users with the "display_name" not capitalized. Do you know what's wrong? –  user1893 Nov 9 '11 at 16:48
    
It seems to work now, but I used the "nickname" field instead of "display_name." I think because the display name is based on the nickname by default (dynamically) I wasn't able to tell if it was capital or not. Using a field with a preexisting value the function worked. Thanks! –  user1893 Nov 9 '11 at 17:27
add comment

Use this to check the first letter of each word -

if(ucwords($name) === $name) :
    { First letter is capitalised }
else :
    $name = ucwords($name);
endif;

Or you can use ucfirst() for only the first letter of the field.

If you know for sure that you wan't it in upercase though, you don't have to worry about the check, just run do $name = ucwords($name); and PHP wont do anything if it does not need to.

share|improve this answer
    
Thanks for the help David! –  user1893 Nov 9 '11 at 16:01
    
After running the your if statement, I get results that show that all of the users' field is not capitalized and I don't understand why is that... –  user1893 Nov 9 '11 at 16:56
    
Figured out the problem. I needed to use a field an existing value in it. I used the nickname instead of display_name (as I was using in my code). Thanks a lot. –  user1893 Nov 9 '11 at 17:29
    
You are welcome. –  David Gard Nov 10 '11 at 17:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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