【问题标题】:Ninja Forms Populate Select with SQL/PHPNinja 表单使用 SQL/PHP 填充 Select
【发布时间】:2017-06-09 02:09:27
【问题描述】:

场景: 我将 Wordpress 与 Ninja Forms 结合使用,我想用 PHP/SQL 填充选择列表。

我创建了一个带有选择字段的简单表单,如果可能的话,我想使用 PHP 和 SQL 来填充它,而不使用 jQuery。

浏览了 Ninja Form API 和其他资源后,我没有找到答案。

使用这些代码 sn-ps 我可以填充文本框和文本区域字段,但不能填充选择字段。

如果需要更多信息,我可以发布!

Code snippet Ninjaforms

And this one that works with custom post types

    // filter
    add_filter( 'ninja_forms_render_default_value', 'my_change_nf_default_value', 10, 3 );
    function my_change_nf_default_value( $default_value, $field_type, $field_settings, $default_option, $field_id ) {
      if( $fh = fopen( "/var/www/virtual/wp-content/themes/xxxx/error.log", "a+" ) ){ fwrite ( $fh,$field_type."\n"); fclose( $fh ); }
      if( 'textarea' == $field_type ){
        $default_value = 'fooBARRRRRRRRR';
        $options = 'foo';
      }
      return $default_value;
    }
    apply_filters(‘ninja_forms_render_default_value’, $default_value, $field_type,  $field_settings, $field_id, $default_option);

    //Function
    function cpt_prepopulate_forms($options, $settings) {
    global $post;
    if( $fh = fopen( "/var/www/virtual/wp-content/themes/xxxx/error.log", "a+" ) ){ fwrite ( $fh,var_export($data,true)); fclose( $fh ); }
    if( $settings['id'] == 152 ) // change to your field ID
    {  
        $args = array(
            'post_type' => 'cash', // Change to your Custom Post type
        );
        $query = new WP_Query( $args );
        if ( $query->have_posts() ) 
        {
            $options = array();
            while ( $query->have_posts() ) 
            {
                $query->the_post();
                $options[] = array(
                    'label' =>  get_the_title(),
                    'value' =>  $post->post_name,
                    'calc'  =>  null,
                    'selected' => 0
                );
            }
        } 
        wp_reset_postdata();
    }
    return $options;
    $conn->close();
}
add_filter('ninja_forms_render_options','cpt_prepopulate_forms', 10, 2);
// end

这可行,但我只能填充文本区域或文本字段/电子邮件,但不能填充选择/下拉列表。表单在加载时由 Ninjaforms 生成,因此无法将自定义 Php 放在 <select></select> 之间。

【问题讨论】:

    标签: php mysql ninja-forms


    【解决方案1】:

    这有帮助吗? https://etzelstorfer.com/en/create-dynamic-ninja-forms-lists-post-types/

    /******************************************
    * SHOW ALL ROOMS IN FIELD WITH KEY "ROOMS"
    ******************************************/
    add_filter( 'ninja_forms_render_options', function($options,$settings){
       if( $settings['key'] == 'rooms' ){
           $args = array(
               'post_type' => 'room',
               'orderby' => 'menu_order',
               'order' => 'ASC',
               'posts_per_page' => 100,
               'post_status' => 'publish'
           );
           $the_query = new WP_Query( $args ); 
           if ( $the_query->have_posts() ){
               global $post;
               while ( $the_query->have_posts() ){
                   $the_query->the_post();
                   $options[] = array('label' => get_the_title( ), 'value' => get_the_title( ));
               }
               wp_reset_postdata(); 
           }
       }
       return $options;
    },10,2);
    

    感谢 Hannes。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多