Registration field – Dropdown and parents
-
Hello,
I’m testing the Ultimate member plugin before purchasing. I can see that the Dropdown fields in the registration form has some sort of a parent and callback value.
I have set up the following scenario:
DropdownCarType: Value1 = Audi
DropdownCarModel: Value1=Q5,Parent=DropdownCarType, Callback=AudiThe DropdownCarmodel now shows when Value=Audi is selected, but my new dropdown containing a few values are shown as empty.
Isn’t this how this functionallity is supposed to work? I didn’t find any kind of documentation regarding this.
Thanks!
-
Hi,
I’m also exploring the choice callback feature and have found very little documentation. In case you’re still looking (or if someone else follows this path), I’ll add what I’ve learned here:
It appears you need to write your own callback function and put it into the
functions.php
script of your theme. The function needs to return an array, and I found that a key-value pair array gave me the most control over the options that are returned.I managed to get a simple example working with 2 dropdown fields:
1. “countries” – just a standard dropdown field with options predefined
2. “cities”:
– Choices Callback = “getCities”
– Parent = “cities”Then, I added my getCities callback to my child-theme’s functions.php file:
The $cities array is returned, and my field “cities” is populated with the options from that array.
The issue I now have is that I can’t get my field with a Choice callback to display on my profile page – the field’s visible in edit mode, and saves correctly to the wp_user-meta table of the database, but won’t show in ‘view’ mode. I’ve double checked the “visibility” option for the field, and it’s set to View Everywhere. As soon as I remove the choice callback and parent field, it’s visible. Anyone know of a solution to this?
~D.
-
This reply was modified 8 years ago by
degami. Reason: fixed gist link
@degami I’m sorry to revive this older post, but I can’t find any documentation whatsoever about the callback function, and it seems to be what I need.
Can you tell me, what exactly did you enter in the “Choices Callback” field? I tried “getCities()” and “getCities();” but no luck.Scratch that, I resolved that issue. (For anyone interested, the callback field should take the name of the function without and punctuation).
But I’m still facing the same problem you mentioned: it’s not displaying on the front end.
Did you manage to get it working?
-
This reply was modified 7 years, 7 months ago by
timafeo.
@timafeo – glad you got it (mostly) working.
@roosalles – agreed: great plugin, wish it had better developer docs.I’m afraid I never properly resolved the issue and couldn’t get the field to display on the front-end. But, I did eventually create a stupidly complex workaround…
Ultimate member has a field type ‘shortcode’ that lets you add the output of a WordPress shortcode into the form. So, I created a custom shortcode (link to gist) to display any piece of user metadata I wanted.
Then, I added a shortcode field just below the field with the choices callback, and used my
[USERMETA meta='cities']
shortcode. I set this new field to only display on view, to give one field that lets the user update the city value in Edit mode and one field to display the city in View mode.But, the shortcode doesn’t display a nice field header like other fields do, so I also added a “Content Block” field to act as a header.
(Actually, my first attempt was to put the shortcode directly into the Content Block, but it that didn’t parse the shortcode properly. I guess this is why they have a separate Shortcode field-type.)
So the final workaround has 3 fields – the original dropdown select field with the choices callback, a shortcode to display the value on the front end, and a content block to give the value a header. Kinda ridiculous, but it works! (Until they fix the issue in an update and I’m left with duplicated fields everywhere… haha)
~D
@degami Thanks for sharing your solution! It will now probably be 2 of us using your approach ??
I’ve submitted a ticket for their support team (I have a license for their bundle add-ons) and will see what they say.Another problem I just noticed is that those parent/child/callbacks fields don’t work as search filters.. I setup 2 dropdowns (parent and child) as filter but the child doesn’t get populated and changing the parent.. so that’s another problem I still have to solve.
I will share the info if I got some good news from them. Thanks!
Hey guys, I THINK I’ve found a workaround for this problem.. All I needed to do was to change my callback function, adding a new “case null” into my switch statement and setup my function parameter to default = null ($country = null). In my case, I have 2 dropdowns, one for Country and another for Cities. Whenever the user changes the Country dropdown, my callback function returns a few cities for the selected country, something like:
function get_cities( $country = null ) { switch( $country ) { case 'Brazil' : $cities = array( "Curitiba" => "Curitiba", "S?o Paulo" => "S?o Paulo", ); break; case 'USA' : $cities = array( "Chicago" => "Chicago", "New York" => "New York", ); break; case null : $user_id = um_profile_id(); $user_city = get_user_meta( $user_id, 'city', true ); $cities = array( $user_city => $user_city ); break; default : $cities = array(); } return $cities; }
Basically, the problem is that there is a filter um_profile_field_filter_hook__select that’s resetting the value when displaying the field on the profile page (view mode). That happens because the plugin calls your callback function expecting it will get an array of choices (ie. cities) but it doesn’t pass the valeu of the parent field to the function.. o.O
So, if your callback function was like mine, with the default returning an empty array, this empty array was being returned to the filter, which was resetting the value of the variable used for displaying the field on the profile page.
Since this filter doesn’t pass any parameter to the callback function, I just setup the param $country to default to null, and then return the correct value of the field (city) in the array..
I am not sure what that could cause in other parts of the plugin, but it worked for me.. Hope that helps
/,,/
Thank you for your example, it helped me to create my own custom callback. The callback works very well on the initial registration screen.
I added in a check to plug in the current club on the profile page (pulled from the user data). However, I still find that on profile
- edit
, the field (club_name) comes back with no values (“No results found”). Even if I alter/change the parent, it still gets no data for fill in.
Can someone tell me what I might be doing wrong?
Here is my code (minimized):
function get_clubs( ) { $user_id = um_profile_id(); $user_district = get_user_meta( $user_id, 'district', true); if (! empty ($user_district)) { $choice = $user_district; } else { $choice = $_POST['parent_option']; } switch( $choice ) { case 'District 1' : $club_name = array( "Club1" => "Club1", "Club2" => "Club2" ); break; case 'District 2' : $club_name = array( "Club1" => "Club1", "Club2" => "Club2" ); break; case 'District 3' : $club_name = array( "Club1" => "Club1", "Club2" => "Club2" ); break; case null : $user_id = um_profile_id(); $user_club_name = get_user_meta( $user_id, 'club_name', true ); $club_name = array( $user_club_name => $user_club_name ); break; default : $club_name = array(); } return $club_name; }
TIA
Try changing the line:
$user_id = um_profile_id(); to $user_id = um_profile( 'ID' );
I had a problem with that function and the only thing I changed was the way I get the profile ID.. it solved my problem but not sure if it will solve yours..
That does not appear to make any difference. The user meta tags are shown properly on the Account page, but any attempt to alter the data breaks the connection.
I will try moving the custom fields to a custom tab to see if that helps.
I found the issue. Apparently, the Profile Tabs extension was inhibiting quite a few things. Not worth the bother. Profile Tabs removed, and all seems to be stable now. I did have to put in code to double check if the user was changing the District so that it would call up the new options.
-
This reply was modified 8 years ago by
- The topic ‘Registration field – Dropdown and parents’ is closed to new replies.