Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom save-function callback example that will capitalize input string value.
<?php
// Create a registration form field with a custom callback
function my_pmprorh_init_must_love_dogs_example_save_function() {
// don't break if Register Helper is not loaded
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// define the fields
$fields = array();
// Basic Text Field Example
$fields[] = new PMProRH_Field(
'pet_name', // input field name, used as meta key
'text', // field type
array(
'label' => 'Pet Name', // field label
'profile' => true, // display on user profile
'save_function' => 'my_capitalize_registration_field_value', // use a custom callback function
)
);
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'checkout_boxes', // location on checkout page
$field // PMProRH_Field object
);
}
}
add_action( 'init', 'my_pmprorh_init_must_love_dogs_example_save_function' );
// Custom callback function to capitalize string value
function my_capitalize_registration_field_value( $user_id, $field_name, $field_value ) {
// Do nothing if no value or if not string
if ( empty( $field_value ) || ! is_string( $field_value ) ) {
return;
}
// Captilize words
$field_value = ucwords( $field_value );
// Sanitize before saving to the database.
$field_value = sanitize_text_field( $field_value );
// Save to the usermeta table
update_user_meta( $user_id, $field_name, $field_value );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.