Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/fb623c4dc3957f1f2b2d94163cd610b7 to your computer and use it in GitHub Desktop.
Save JarrydLong/fb623c4dc3957f1f2b2d94163cd610b7 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_hidden' => ( isset( $_GET['cat'] ) ? sanitize_text_field( $_GET['cat'] ) : '',
);
// Loop through the fields and set the default values.
foreach ( $pmpro_user_fields as $field_group => $fields ) {
foreach ( $fields as $field ) {
// Check if a default value was specified for the field name and set the field value.
if ( isset( $default_values[ $field->name ] ) ) {
$field->value = esc_attr( $default_values[ $field->name ] );
}
}
}
}
add_action( 'init', 'my_pmpro_user_fields_default_value' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment