• Hi,

    I think there’s a bug with the use of the “Choices Callback” field.

    If we set a function in the field, everything works fine, the values are populated according to the parent option field value.
    But the field for which we set this Choices Callback function becomes not visible in the profile form in “view mode”! (in edit mode the field ramain visible)
    If we remove the function in Choices Callback, the field immediately becomes visible in view mode again.
    This behaviour is easy to reproduce.

    Is the bug registered?
    Any fix on the way?

    Thanks!

    • This topic was modified 5 years, 11 months ago by ftpwp.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter ftpwp

    (@ftpwp)

    Same issue here man.

    I opened a ticket and waiting for their reply.

    Same issue here man.

    I opened a ticket and waiting for their reply.

    Same Issue for me. The field for which we have set this Choices Callback function is not visible on the Default Profile form in view mode.

    NOTE: if you leave the Parent Option field blank, the field will be visible in View Mode.

    Still buggy, but at least that is an option, if you don’t need to specify a Parent

    @artmerk how do you now get the value of the parent option without specifying it in the parent option field… what data will the choices callback use to perform its logic

    somebody help me here i am getting frustrated with this

    @nathekame
    There is no documentation on this that I can find…. but rather than specify a parent option, you can define those parent choices in your custom function (since you write the function to do anything you want). I am successfully using a callback function with no Parent Option.

    So try this:
    1) create a custom function,
    2) reference the function in the field called “Choices Callback”, and
    3) Leave the “Parent Option” field blank

    Then UM reads the function, and dynamically creates drop-down choices in an array (such as 1, Roberts; 2, Jones; 3, Smith) and then stores the array somewhere, and stores the index value in the user_meta field we chose

    Another useful link:
    https://wordpress.org/support/topic/please-explain-choices-callback/

    • This reply was modified 5 years, 4 months ago by artmerk.

    Hello @nathekame,

    Do you have any exemple of code to me? I have the same problem, but I almost have no knowledge of php.

    Thanks,

    Rachel

    @rachelflara please find below the code i implemented in my case. i built a form that requires filling of state and Local government area(LGA) which is more like state and province in a place like america. so i create a parent dropdown with a list of all states in my country and then wrote a function in my functions.php file using a plugin called “code snippet”. and after i wrote the code and injected it with code snippet, i went over to the form in the LGA dropdown, i put getLGA in the choices callback field (please note that i wrote the function without the brackets) and under the options section you put a * sign because that portion cant be left empty. and then on the parent option section i selected state option . i hope this helps, goodluck

    function getLGA() {

    global $wpdb;

    //get the value from the ‘parent’ field, sent via the AJAX post.
    $choice = $_POST[‘parent_option’];

    $lgaList = array();

    $results = $wpdb->get_results( $wpdb->prepare( “SELECT DISTINCT lga FROM pu_data WHERE state=%s”, $choice));

    foreach ( $results as $lgas ) {

    $lgaList[$lgas->lga] = $lgas->lga;

    }

    return $lgaList;

    }

    Thread Starter ftpwp

    (@ftpwp)

    Hi @artmerk,

    > …rather than specify a parent option, you can define those parent
    > choices in your custom function (since you write the function to do
    > anything you want).

    Looks interesting, thanks!
    But can you please give an example?

    Currently my function get the value of the field set in Parent option via this line:

    $choice = $_POST['parent_option'];

    So I understand you suggest to bypass the Parent option to directly get the value from the field that was set in Parent option.

    In my case, the Parent option refers to the Address field. And its meta key is “profile_address”.
    But if I try :

    $choice = $_POST['profile_address'];

    It doesn’t work.

    So can you give an example of code of how you get the value selected in the field that was previously set in Parent option?

    Thanks!

    Hi @ftpwp

    For example, here is a function that should work:

    function get_username() {
      // sort by first name
      $users = get_users(array(
        'meta_key' => 'first_name',
        'orderby' => 'meta_value',
      ));
      foreach ( $users as $user ) {
          $user_names[$user->display_name] = $user->display_name;
      }
      return $user_names;
    }
    

    So in this example, Choices Callback field would say “get_username” and leave the Parent Option as No selected.

    Thread Starter ftpwp

    (@ftpwp)

    Thanks for your answer, but I’m not sure to understand.
    What are your 2 fields in your UM form?

    In my case:
    – Address
    – Floor

    According to the selected address, I have a specific list of floors (different buildings)

    So to build the right Floor list, I need to get which Address has just been selected.

    Now for you?
    Maybe:

    – Sort criteria
    – Usernames

    According to the selected sort criteria, you have your username list sorted as you want? Is that it?
    I’m not sure as ‘first_name’ is already hard coded :-/

    I don’t see the conditional link in between 2 fields (parent and child)
    And I don’t see how you get the value just selected on the form :-/

    Hi @nathekame,

    I have a similar callback function that get datas from the database depending on the parent_option value and I did as you suggested, putting a * in the “Edit choices” as it can’t be empty.

    In my form, my child dropdown is filled with all options from my callback function, matching with my parent dropdown selection, so everything is OK for the form display.
    The problem I am facing is that after submitting the form, this value is not saved in my user meta.

    After some tests, I arrived to the conclusion that if the selected value does not match with a value set in the “Edit choices” field of the dropdown settings, then it is not saved in user meta.
    It is a problem for me as I want my dropdown to be filled with options that can be inserted in my db by other users.

    Don’t you face the same problem with your LGA dropdown ?

    Thanks,

    According to @ultimatemembersupport, they are aware of the issue and it’s being resolved in the next release.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Bug using Choices Callback: field not visible in view mode’ is closed to new replies.