【问题标题】:Gravity Forms dynamic population and Advanced Custom Fields, choices not showingGravity Forms 动态填充和高级自定义字段,选项未显示
【发布时间】:2017-04-06 09:24:25
【问题描述】:

我正在使用选项页面中的 ACF 字段来填充重力形式下拉菜单。这些值是从字段中提取的,但显示在表单顶部(预览中),而不是下拉列表中。我确定这是我如何构建表单的问题。提前感谢您的帮助!

Screenshot

//gravity forms dynamically populate from ACF

add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );

function populate_posts( $form ) {

    global $choices;

    foreach ( $form['fields'] as &$field ) {

        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
            continue;
        }

        if( have_rows('core_values', 'option') ):

            while( have_rows('core_values', 'option') ): the_row();
                $choices[] = array( 'text' => the_sub_field('value_mstr_name'), 'value' => the_sub_field('value_mstr_name') );
            endwhile;


        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select a Post';
        $field->choices = $choices;

        endif;

    }

    return $form;
}

【问题讨论】:

    标签: php advanced-custom-fields gravity-forms-plugin


    【解决方案1】:

    您也可以将其限制为特定的帖子类型

    add_filter( 'gform_pre_render_3', 'populate_warranty_form' );
    add_filter( 'gform_pre_validation_3', 'populate_warranty_form' );
    add_filter( 'gform_pre_submission_filter_3', 'populate_warranty_form' );
    add_filter( 'gform_admin_pre_render_3', 'populate_warranty_form' );
    function populate_warranty_form( $form ) {
    
    foreach ( $form['fields'] as &$field ) {
        // select field with css class 'product-name'
        // this allows dynamic field to use existing css
        if ( $field->type != 'select' || strpos( $field->cssClass, 'product-name' ) === false ) {
            continue;
        }
    
        $args = array(
            'post_type'     => 'custom_post_type',
            'posts_per_page'=> '-1',
            'post_status'   => 'publish',
            'orderby'       => 'title',
            'order'         => 'ASC'
         );
        $posts = get_posts( $args );
    
        foreach ( $posts as $post ) {
            $postID = $post->ID;
    
            if( have_rows('repeater_field', $postID) ): while( have_rows('repeater_field', $postID) ): the_row();
                $choices[] = array( 'text' => get_sub_field('repeater_sub_field'), 'value' => get_sub_field('repeater_sub_field') );
            endwhile; endif;
        }
    
    
        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select';
        $field->choices = $choices;
    
    }
    
    return $form;
    }
    

    【讨论】:

      【解决方案2】:

      我没有将选项定义为数组,对于 ACF,函数 the_sub_field 就像 sub_field 的回声,但 get_sub_field 实际上将其传播到 GF 字段对象选项属性中。

      //gravity forms dynamically populate from ACF
      
      add_filter( 'gform_pre_render_1', 'populate_posts' );
      add_filter( 'gform_pre_validation_1', 'populate_posts' );
      add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
      add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
      
      function populate_posts( $form ) {
      
          foreach ( $form['fields'] as &$field ) {
      
              if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
                  continue;
              }
      
              if( have_rows('core_values', 'option') ):
      
              $choices = array();
      
                  while( have_rows('core_values', 'option') ): the_row();
                      $choices[] = array( 'text' => get_sub_field('value_mstr_name'), 'value' => get_sub_field('value_mstr_name') );
                  endwhile;
      
      
              // update 'Select a Post' to whatever you'd like the instructive option to be
              $field->placeholder = 'Select a Core Value';
              $field->choices = $choices;
      
              endif;
      
          }
      
          return $form;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-18
        • 2021-11-25
        • 2014-03-23
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        相关资源
        最近更新 更多