Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter whoaloic

    (@whoaloic)

    For now, I succeed to get post types:

    function cf7_dynamic_select() {
    
        $output = array('área de atua??o/Segmento' => '0');
       $args = array( 'post_type' => 'wpsl_stores','posts_per_page' => -1, 'meta_key' => 'wpsl_zip','orderby'  => array(
    		'meta_value_num' => 'ASC',
    		'post_date'      => 'ASC',
    	), );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata($post);
           $title = get_the_title($post->ID);
    	   $zip = get_post_meta($post->ID, 'wpsl_zip', true);
    	   $city = get_post_meta($post->ID, 'wpsl_city', true);
    	   $email = get_post_meta($post->ID, 'wpsl_email', true);
    	   $output[$zip . ' '. $city .' - ' . $title .' | '. $email] = $post->ID;
        endforeach;
        return $output;
    }
    add_filter('my-filter', 'cf7_dynamic_select', 10, 2);

    I sill need to trim content after the pipe and get $email in the “To” field in CF7 Email tab.
    https://contactform7.com/selectable-recipient-with-pipes/

    Plugin Author John Huebner

    (@hube2)

    This plugin does not allow for pipes. To do what you’re doing I would use a combination of this plugin and my other plugin https://www.ads-software.com/plugins/contact-form-7-dynamic-mail-to/. This plugin would load the labels with values of post IDs like you are doing and then then other plugin would take that post ID, get the email address and send the email to that recipient.

    Thread Starter whoaloic

    (@whoaloic)

    I finally use:

    function dynamic_select_list($tag, $unused) {
    
    		$options = (array)$tag['options'];
    
    		foreach ($options as $option) {
    			if (preg_match('%^from_db:([-0-9a-zA-Z_]+)$%', $option, $matches)) {
    				$from_db = $matches[1];
    			}
    		}
    
    		if(!isset($from_db)) {
    			return $tag;
    		}
    
    		// get the existing WPCF7_Pipe objects
    		$befores = $tag['pipes']->collect_befores();
    		$afters = $tag['pipes']->collect_afters();
    
    		// add the existing WPCF7_Pipe objects to the new pipes array
    		$pipes_new = array();
    		for ($i=0; $i < count($befores); $i++) {
    			$pipes_new[] = $befores[$i] . '|' . $afters[$i];
    		}
    
    		// Add stores to the $tag values
    		if($from_db === 'stores') {
    			 global $post;
        $args = array( 'post_type' => 'wpsl_stores','posts_per_page' => -1, 'meta_key' => 'wpsl_zip','orderby'  => array(
    		'meta_value_num' => 'ASC',
    		'post_date'      => 'ASC',
    	), );
        $myposts = get_posts( $args );
    			if (!$myposts) {
    				return $tag;
    			}
    
    			foreach ($myposts as $post) {
    				$title = get_the_title();
    	   $zip = get_post_meta($post->ID, 'wpsl_zip', true);
    	   $city = get_post_meta($post->ID, 'wpsl_city', true);
    	   $email = get_post_meta($post->ID, 'wpsl_email', true);
                $tag['raw_values'][] = $email;
                $tag['values'][] = $title;
                $tag['labels'][] = $zip . ' '. $city .' - ' . $title;
                // add the store info for use as a new WPCF7_Pipe object
                $pipes_new[] = $title . '|' . $email;
            }
            wp_reset_postdata();
        }
    
    		// setup all the WPCF7_Pipe objects
    		$tag['pipes'] = new WPCF7_Pipes($pipes_new);
    
    		return $tag;
    	}
    	add_filter('wpcf7_form_tag', 'dynamic_select_list', 10, 2);

    Then I add shortcode in contact form tab:
    [select* store default:get id:store class:store from_db:stores]
    And [store] in the “To” field of Email tab.
    Also “Magasin : [_raw_store] in the content Email tab to get the name of the store

    Thanks to default:get parameter it is possible to get an URL link of items in the dropdown.

    Now I’m looking for a way to get this:
    The Post store ID as option value but still get store label in the email content.
    <select>
    <option value=”1″>Store 1</option>
    <option value=”2″>Store 2</option>
    </select>

    Thread Starter whoaloic

    (@whoaloic)

    It seems that $tag[‘values’][] and $befores = $tag[‘pipes’]->collect_befores(); have to match.

    Plugin Author John Huebner

    (@hube2)

    I’m not sure what you’re doing to be honest. This plugin has limited capabilities and it does not support pipes. The only thing that it can do is to display a list of value => label pairs in a select box and to show the selected value in the in the email content, not the label for that value. What you’re attempting to do is beyond the ability of this plugin.

    I have had requests to allow pipes and for adding the label instead of the value to email content, however, I have not been able to figure out how to do so, mostly because the documentation for extending CF7 is non-existent and I was unable to figure out what changes need to be made by digging through the CF7 code.

    Yes, it would be a useful addition, and if anyone wants to tackle it I would give them credit for the work and I will accept pull requests. https://github.com/Hube2/contact-form-7-dynamic-select-extension/issues/1

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Select recipients from a custom post type’ is closed to new replies.