1

I looked around here and by search engine, but unfortunately I couldn't find a solution for myself.

So, I now ask assistance with a function that I need to customize for the Contact Form 7 WordPress plugin. The function was from another question.

In a drop-down menu (select) I need two details (workshop name and date) in one option field. Both details come from the same post of a custom post type. The first detail is a post_title, the second is a custom-field from Meta-Box plugin.

The following function works in principle, but it only returns the one or the other detail. Probably the solution is within the foreach construct. But I don't know how it works.

I would be very grateful for support!

[UPDATE 2018-08-12] After further research, I've found the solution at this post and changed the function accordingly.

The solution should look like this:

<select>
<option value="workshop name – date">workshop name – date</option>
...
</select>

This is the function:

add_filter( 'wpcf7_form_tag', 'dynamic_field_choose_workshop', 10, 2);

function dynamic_field_choose_workshop ( $tag, $unused ) {

    if ( $tag['name'] != 'workshop' )
        return $tag;

    $args = array (
        'post_type'     => 'workshop',
        'post_status'   => 'publish',
        'orderby'       => 'name',
        'order'         => 'ASC',
        'numberposts'   => - 1,
    );

    $custom_posts = get_posts($args);

    if ( ! $custom_posts )
        return $tag;

    foreach ( $custom_posts as $custom_post ) {
    $ID = $custom_post->ID;
        $tag['values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
        $tag['raw_values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
        $tag['labels'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
    }

    return $tag;

}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
Mika L.
  • 11
  • 1
  • 3

1 Answers1

0

There is CF7 extension that will do this for you. Checkout the Smart Grid-Layout for CF7, it introduces a new tag called dynamic_dropdown. This is is what you want to use. The dynamic_dropdown creates a select field and allows you to populate the field options using either a taxonomy, titles of a post type, or a filter. You want to use the filter option to actually construct the options as per your requirement. The tag popup window is self explanatory, however if you get stuck post a comment below and I'll give you some more tips.

Using the following dynamic_dropdown tag,

[dynamic_select workshop-date-select class:select2 "source:filter"]

it creates a <select name="workshop-date-select"> dropdown field which will be converted into a select2 jquery field on the front end, and its values dynamically created using the following function placed in the functions.php file,

add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
  /*first we verify if this is the right field from the right form
  in case multiple forms with similar fieldd exiss.
  the $form_key is a unique key exposed by the Smart Grid-layout plugin 
  instead of using form IDs to make forms and code more portable across servers.*/
  if($form_key != 'my-form' && $field_name != 'workshop-date-select') return $options; 
  $options = array();
  //load your options programmatically, as $value=>$name pairs.
  $args = array (
    'post_type'     => 'workshop',
    'post_status'   => 'publish',
    'orderby'       => 'name',
    'order'         => 'ASC',
    'numberposts'   => - 1,
  );
  $workshops = get_posts( $args );
  foreach($workshops as $workshop){
    $val = $workshop->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $workshop->ID);
    $options[$val]=$val;
  }
  return $options;
}

this will create the desired dropdown select field in the front end.

NOTE of CAUTION: I would populate the option values as the workshop post ID rather than the same text as the option label. When the form is submitted the value of the post ID can be used to populate the notification email with the desired workshop title and date. This gives more flexibility to expand the reported information in the future.

Aurovrata
  • 2,000
  • 27
  • 45
  • Thank you for hinting at your WP plugin. For now it is working fine and I want to do it without another plugin. But I'll save the link. ;-) – Mika L. Aug 22 '18 at 09:50
  • You're wc @MikaL. it was designed with this kind of problem in find among other things. – Aurovrata Aug 23 '18 at 11:07