• I’ve been looking all over the place for a way to populate a listbox with post-titles from a certain category. Various hacks, do populate a listbox, but don’t send the actual selected value or mess up all other listboxes.
    I found this hack for the select.php module:

    1.
    In function wpcf7_select_shortcode_handler, $values, $labels and $defaults need to be redefined for your customised dropdown. They are arrays for dropdown values, dropdown texts and default selection respectively. I have the following snippet right after $options are parsed (after line 50). Here my select field ID is “position”.

    if ($name == 'position') {
      // query the positions
      $positions = new WP_Query('YOUR_QUERY');
    
      while( $positions->have_posts() ) : $positions->the_post();
        array_push( $values, get_the_ID() );     // post ID as option value
        array_push( $labels, get_the_title() );  // post title as option text
      endwhile;
    
      // set the key of the default selection
      array_push( $defaults, 1 + array_search($YOUR_DEFAULT_VALUE, $values) );
    }

    2.
    In function wpcf7_select_validation_filter, you need to re-fill the dropdown option values before the post operation because they cannot be found in the shortcode. They will be needed in $_POST later. I have the following snippet right after the local variable definitions. The code is very similar.

    if ($name == 'position') {
      // query the positions
      $positions = new WP_Query('YOUR_QUERY');
      while( $positions->have_posts() ) : $positions->the_post();
        array_push( $values, get_the_ID() );
      endwhile;
    }

    It works and does populate the listbox with my post-titles, but emails me the post-ID.
    I can’t figure out what to change to get the titles in the email instead of the ID.

    Can anybody help me with this?
    Thanx in advance!

  • The topic ‘[Plugin: Contact Form 7] Populate listbox with post-titles’ is closed to new replies.