Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save denisbaranov/856cead85bef583f3fd7c3559432fbf1 to your computer and use it in GitHub Desktop.
Save denisbaranov/856cead85bef583f3fd7c3559432fbf1 to your computer and use it in GitHub Desktop.
This example shows how to add custom predefined field that extends the 'checkbox' field type in Ultimate Member
<?php
/**
* This example shows how to add custom predefined field that extends the 'checkbox' field type in Ultimate Member
* You can add custom code to the end of the file functions.php in the active theme directory.
* Important! You have to change variables: $type, $title, $options
*
* @author Ultimate Member support <support@ultimatemember.com>
* @package Ultimate Member
* @since 2020-06-25
* @see #40961
*/
if ( function_exists( 'UM' ) ) {
/* YOU SHOULD SET FIELD TYPE, TITLE AND OPTIONS HERE */
$type = $metakey = 'um_business_type';
$title = __( 'Business type', 'ultimate-member' );
$options = array(
'business type 01',
'business type 02',
'business type 03'
);
/* add new predefined field */
add_filter( 'um_predefined_fields_hook', function( $predefined_fields ) use($metakey, $options, $title, $type) {
$predefined_fields[$type] = array(
'type' => 'checkbox',
'title' => $title,
'label' => $title,
'metakey' => $metakey,
'public' => 1,
'required' => 0,
'editable' => 1,
'options' => $options
);
return $predefined_fields;
}, 20 );
/* set default options */
$saved_fields = get_option( 'um_fields' );
if ( isset( $saved_fields[$metakey] ) && ( empty( $saved_fields[$metakey]['options'] ) || $saved_fields[$metakey]['options'] !== $options ) ) {
$saved_fields[$metakey]['options'] = $options;
update_option( 'um_fields', $saved_fields );
}
/* override options on view and edit */
add_filter( "um_get_field__$metakey", function( $array ) use( $options ) {
$array['options'] = $options;
return $array;
}, 20 );
/* override options on save */
add_filter( 'um_user_edit_profile_fields', function( $fields ) use( $metakey, $options ) {
if ( isset( $fields[$metakey] ) ) {
$fields[$metakey]['options'] = $options;
}
return $fields;
}, 20 );
/* use this field as a member directory filter */
if ( isset( UM()->member_directory()->filter_fields ) ) {
UM()->member_directory()->filter_fields[$metakey] = $title;
}
}
@chunbaoapp
Copy link

Hi, I have a similar question:
How to overwrite predefined countries? I tried a few times, but couldn't figure it out. Can you help me?

@csilm
Copy link

csilm commented Feb 29, 2024

Similar Problem. but for Images. How do you extend the image field to take multiple images as input?

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