Skip Navigation

[Resolved] Make sure the value of a custom field is always capitalized

This thread is resolved. Here is a description of the problem and solution.

Problem:
Automatically capitalise the first letter of a custom field submitted with a Toolset Form.

Solution:
You can use the cred_save_data hook to manipulate the field value—capitalising the first letter—with some custom code, like so:

function tssupp_capitalise_field( $post_id, $form_data ){
 
    if ( in_array( $form_data['id'], array( 98, 216 ) ) ) { // Edit form IDs
 
        $slug = 'some-field'; // Edit field slug
 
        $field = get_post_meta( $post_id, 'wpcf-' . $slug, true );
        $field = ucfirst($field);
        update_post_meta( $post_id, 'wpcf-' . $slug, $field );
    }
}
add_action( 'cred_save_data', 'tssupp_capitalise_field', 10, 2 );

Relevant Documentation:
https://toolset.com/documentation/adding-custom-code/

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Nicholas 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1098470

Hello I'd like to achieve something similar but for a custom field

https://toolset.com/forums/topic/make-sure-post-title-is-always-capitalized/

Regards,
Nick

#1098573

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Is this for front-end submissions only?

It only requires very modest changes of the solution from your other thread for the post title.

Try this:

function tssupp_capitalise_field( $post_id, $form_data ){

	if ( in_array( $form_data['id'], array( 98, 216 ) ) ) { // Edit form IDs

		$slug = 'some-field'; // Edit field slug

		$field = get_post_meta( $post_id, 'wpcf-' . $slug, true );
		$field = ucfirst($field);
		update_post_meta( $post_id, 'wpcf-' . $slug, $field );
	}
}
add_action( 'cred_save_data', 'tssupp_capitalise_field', 10, 2 );
#1102343

Thanks Nigel.