PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Can Radio Buttons be dynamically populated by a custom field array

  1. thirtypointfour
    Member

    I am building a tour company website and I'm using a custom post type called "Tours" which has a number of custom fields associated with it.

    One of the custom fileds is used to store multiple "Departure Dates" associated with the tour. I'm using the Deluxe Blog Tips Meta Box script which allows you to clone a custom fields (in the backend) thus creating an array of values associated with the one custom field. For example I am currently outputting these values as follows in the template:

    $metas = get_post_meta( get_the_ID(), 'tour_dates', true );
    foreach ( $metas as $meta )
    { echo $meta; }

    This Outputs a simple list of dates (or what ever is added in the custom field) such as:

    29th September 2012
    15th October 2012
    23rd November 2012

    What I am wanting to do is put a simple pre-booking form at the bottom of the tour page. On this form I want users to be able to select a tour date they want to book.

    Finally the question:

    Is it possible to dynamically populate a Radio Button list with the various departure dates (any value) coming from an array of values stored in a single custom field?

    Ideally the form will have a hidden field populated by the post title (that bits easy) and the pre populated radio button list (thats where I am stuck!!) for the user to select the dates that suit them.

    If this is possible I'll simply pass the Page Title & The Selected Departure Date to a generic booking form, (on another page) where the client can fill out all their contact details ect, and keep the main tour page simple.

    Would be great to hear if this is possible and if anyone has done anything similar.

    Posted 10 years ago on Wednesday November 14, 2012 | Permalink
  2. thirtypointfour
    Member

    I have managed to almost do this bar one error when visiting the form in the backend!!

    Warning: Invalid argument supplied for foreach() on line 19

    this refers to the line:

    foreach ( $metas as $meta )'

    Below is what I am using which currently works front end but unfortunately is throwing up an error when I go to the form in the backend of wordpress:

    add_filter("gform_pre_render", "populate_radio_buttons");
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_radio_buttons");
    
    function populate_radio_buttons($form){
    
        //only populating radio buttons in form id 5
        if($form["id"] != 5)
    
        return $form;
    
        //Creating radio buttons item array.
        $items = array();
    
        //To add an initial value uncomment this next line
        //$items[] = array("text" => "Please Select", "value" => "");
        // Get the custom field values stored in the array
        $metas = get_post_meta( get_the_ID(), 'tour_dates', true );
        foreach ( $metas as $meta )
        // Add the Values to the drop down
        $items[] = array("value" => $meta, "text" => $meta);
    
        //Adding items to field id 7. Replace 7 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 7){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    Any thoughts on this?

    Posted 10 years ago on Wednesday November 14, 2012 | Permalink
  3. thirtypointfour
    Member

    All Fixed!

    Simply replace lines 19,20 & 21 with the following:

    if (is_array($metas))
    {
    foreach($metas as $meta)  $items[] = array("value" => $meta, "text" => $meta);
    }

    So the whole function should then look like this:

    add_filter("gform_pre_render", "populate_radio_buttons");
    
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_radio_buttons");
    
    function populate_radio_buttons($form){
    
        //only populating drop down for form id 5
        if($form["id"] != 5)
    
        return $form;
    
        //Creating radio button item array.
        $items = array();
        // Get the custom field values stored in the array
        $metas = get_post_meta( get_the_ID(), 'tour_dates', true );
    
    if (is_array($metas))
    {
    foreach($metas as $meta)  $items[] = array("value" => $meta, "text" => $meta);
    }
        //Adding items to field id 10. Replace 10 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 10){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    Hope this helps others with the same needs.

    Posted 10 years ago on Thursday November 15, 2012 | Permalink
  4. I'm certain your code will help someone. Looks like a very nice solution. Thank you for sharing it.

    Posted 10 years ago on Saturday November 17, 2012 | Permalink