php 使用Ninja Forms选择字段预填充

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用Ninja Forms选择字段预填充相关的知识,希望对你有一定的参考价值。

<?php

add_filter('ninja_forms_render_options','fc_pre_population_callback', 10, 2);
function fc_pre_population_callback($options, $settings) {
  
  	if (is_singular('contact-forms')) {

  		/*// target only the field with this key
	  	if( $settings['key'] == 'business_location' ) {

		    // write the query to fetch the data
		    $args = array(
		      'post_type' => 'contact-forms',
		      'post_status' => 'publish'
		    );

		    $posts = new WP_Query( $args );

		    if ( $posts->have_posts() ) {

		      $options = array();

		      while ( $posts->have_posts() ) {

		        $posts->the_post();
		        
		        // $options is the variable which contains tha values rendered
		        // we will use the post title as lable and the ID as value
		        $options[] = array(
		          'label' => get_the_title(),
		          'value' => get_the_ID()
		        );
		      } // endwhile
		    } // endif

		    wp_reset_postdata();
		}

		$options = [
		  "0" => [
		     "label" => 'Texas',
		     "value" => 'yhaouatis@gmail.com,zakyhaouatis@gmail.com'
		  ],
		  "1" => [
		     "label" => 'Huston',
		     "value" => 'zakyhaouatis@gmail.com,yhaouatis@gmail.com'
		  ]
		];
		*/

		$locations = get_field('business_locations', get_the_ID());
		$options = array();

		foreach($locations as $location) {
			$options[] = array(
				'label' => $location['location'],
				'value' => $location['emails']
			);
		}

  	}
  
   	return $options;
}

以上是关于php 使用Ninja Forms选择字段预填充的主要内容,如果未能解决你的问题,请参考以下文章

WordPress Ninja Forms 日期选择器

如何使 Ninja Forms 3.1 中的字段唯一?

markdown 在Ninja Forms中定位字段

Ninja 表单使用 SQL/PHP 填充 Select

如何使用jquery预填充字段

jQuery 和 Ninja Forms WordPress 插件