Added the dynamic select
This commit is contained in:
		
							parent
							
								
									1b9c1e5ea5
								
							
						
					
					
						commit
						a5e9e68294
					
				
					 1 changed files with 67 additions and 0 deletions
				
			
		
							
								
								
									
										67
									
								
								dynamic-select-plugin/cklph-ff-custom-select.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								dynamic-select-plugin/cklph-ff-custom-select.php
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,67 @@
 | 
			
		|||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * Plugin Name: Chykalophia Custom Select for Fluent Forms
 | 
			
		||||
 * Description: An extension plugin for fluentforms that generates a dynamic select from specific custom post types.
 | 
			
		||||
 * Source: https://fluentforms.com/docs/fluenform_rendering_field_data_select/
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The code that runs during plugin acivation and deactivation
 | 
			
		||||
 */
 | 
			
		||||
function cklph_ff_custom_select_activate_deactivate() {
 | 
			
		||||
	flush_rewrite_rules();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
register_activation_hook( __FILE__, 'cklph_ff_custom_select_activate_deactivate' );
 | 
			
		||||
register_deactivation_hook( __FILE__, 'cklph_ff_custom_select_activate_deactivate' );
 | 
			
		||||
 | 
			
		||||
/** A filter used in fluent forms to update a select field */
 | 
			
		||||
add_filter( 'fluentform_rendering_field_data_select', function ( $data, $form ) {
 | 
			
		||||
 | 
			
		||||
	// Used to identify the form id for the dynamic select.
 | 
			
		||||
	if ( $form->id != 7 ) {
 | 
			
		||||
		return $data;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// check if the name attriibute of the select field in fluent forms is 'dynamic_dropdown'.
 | 
			
		||||
	if ( \FluentForm\Framework\Helpers\ArrayHelper::get( $data, 'attributes.name' ) != 'dynamic_dropdown' ) {
 | 
			
		||||
		return $data;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	$product_titles = get_product_titles();
 | 
			
		||||
	$dropdown_data = array();
 | 
			
		||||
 | 
			
		||||
	foreach ( $product_titles as $product_title ) {
 | 
			
		||||
		$dropdown_data[] = array(
 | 
			
		||||
			'label'      => esc_attr( $product_title ),
 | 
			
		||||
			'value'      => esc_attr( $product_title ),
 | 
			
		||||
			'calc_value' => '1',
 | 
			
		||||
		);
 | 
			
		||||
	}
 | 
			
		||||
	// We are merging with existing options here.
 | 
			
		||||
	$data['settings']['advanced_options'] = $dropdown_data;
 | 
			
		||||
 | 
			
		||||
	return $data;
 | 
			
		||||
}, 10, 2);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Custom function to get the product titles of a CPT. For this example, gets product CPT's.
 | 
			
		||||
 *
 | 
			
		||||
 * @return String array of CPT titles
 | 
			
		||||
 */
 | 
			
		||||
function get_product_titles() {
 | 
			
		||||
	$titles = array();
 | 
			
		||||
	$args   = array(
 | 
			
		||||
		'post_type'      => 'product',
 | 
			
		||||
		'posts_per_page' => 10,
 | 
			
		||||
	);
 | 
			
		||||
	$loop = new WP_Query( $args );
 | 
			
		||||
	while ( $loop->have_posts() ) {
 | 
			
		||||
		$loop->the_post();
 | 
			
		||||
		$titles[] = get_the_title();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return $titles;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
$data = apply_filters( 'fluentform_rendering_field_data_select', $data, $form );
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue