Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active June 6, 2024 16:16
Show Gist options
  • Save ipokkel/2f397787ad18abd9fdf68bde627fcc63 to your computer and use it in GitHub Desktop.
Save ipokkel/2f397787ad18abd9fdf68bde627fcc63 to your computer and use it in GitHub Desktop.
Set default field values for PMPro User Fields.
<?php
/**
* Set default values for PMPro user fields.
*
* Supported field types: text, textarea, checkbox, radio, select, select2, multiselect, number, and date.
* Can support readonly and hidden fields, however, they will not be editable by the user.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_user_fields_default_value() {
global $pmpro_user_fields;
// Set default values for the fields. Format: 'field_name' => 'default value'
$default_values = array(
'example_text' => 'default value',
'example_textarea' => 'Row one.' . "\r\n" . 'Row two.' . "\r\n" . 'Row three.',
'example_checkbox' => '1',
'example_radio' => 'no',
'example_select' => 'Beta',
'example_select2' => array( 'Beta', 'Charlie' ),
'example_multiselect' => array( 'Alpha', 'Beta' ),
'example_number' => '123',
'example_date' => '2023-05-28', // Format: YYYY-MM-DD
'example_readonly' => 'This is a readonly field.',
'example_hidden' => 'I am hidden.',
);
// Loop through the fields and set the default values.
foreach ( $pmpro_user_fields as $field_group => $fields ) {
foreach ( $fields as $field ) {
// Bail if field type is not supported.
if ( ! in_array( $field->type, array( 'text', 'textarea', 'checkbox', 'radio', 'select', 'select2', 'multiselect', 'number', 'date', 'readonly', 'hidden' ), true ) ) {
continue;
}
// Check if a default value was specified for the field name and set the field value.
if ( isset( $default_values[ $field->name ] ) ) {
// Bail if value is not a string and field type is not multiselect or not select2
if ( ! is_string( $default_values[ $field->name ] ) && ! in_array( $field->type, array( 'multiselect', 'select2' ), true ) ) {
continue;
}
// Remove html tags from string or array values.
if ( is_string( $default_values[ $field->name ] ) ) {
$default_values[ $field->name ] = wp_strip_all_tags( $default_values[ $field->name ] );
} elseif ( is_array( $default_values[ $field->name ] ) ) {
foreach ( $default_values[ $field->name ] as $key => $value ) {
$default_values[ $field->name ][ $key ] = wp_strip_all_tags( $value );
}
}
// Bail if date is not in the correct format.
if ( 'date' === $field->type && ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $default_values[ $field->name ] ) ) {
continue;
}
// remove all non-numbers from string if field type is number.
if ( 'number' === $field->type ) {
$default_values[ $field->name ] = preg_replace( '/[^0-9]/', '', $default_values[ $field->name ] );
}
// if field has options, remove array values that are not in the field options.
if ( ! empty( $field->options ) ) {
if ( 'multiselect' === $field->type ) {
$default_values[ $field->name ] = array_intersect( $default_values[ $field->name ], $field->options );
} elseif ( 'select2' === $field->type ) {
$default_values[ $field->name ] = array_intersect( $default_values[ $field->name ], array_keys( $field->options ) );
} else {
$default_values[ $field->name ] = in_array( $default_values[ $field->name ], $field->options, true ) ? $default_values[ $field->name ] : '';
}
}
// Set default value if it's not already set.
if ( empty( $field->value ) ) {
$field->value = esc_attr( $default_values[ $field->name ] );
}
}
}
}
}
add_action( 'init', 'my_pmpro_user_fields_default_value' );
@ipokkel
Copy link
Author

ipokkel commented Dec 14, 2022

example-user-fields-setup

@ipokkel
Copy link
Author

ipokkel commented Dec 14, 2022

example-user-fields-render

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment