【问题标题】:Dynamically pre-populate Gravity Forms field with custom fields from custom post types使用来自自定义帖子类型的自定义字段动态预填充重力表单字段
【发布时间】:2015-05-03 04:00:05
【问题描述】:

试图在标题中尽可能具体。本质上,我有一个正在开发的房地产网站,其中包含房地产清单。属性列表是一种自定义帖子类型,并且附加了几个其他自定义帖子类型(每个都有自己的自定义字段)。

每个属性都有一个附加的代理(自定义帖子类型),带有自定义字段,例如电子邮件、Facebook、电话号码等。

我需要使用代理电子邮件(与作者电子邮件不同)动态预填充重力表单上的隐藏字段,以便我可以向该地址发送电子邮件。

我尝试了以下操作,但没有成功,因为我确定从代理自定义帖子类型调用自定义字段时缺少某些内容,但我不确定如何。这是我目前正在使用的:

add_filter('gform_field_value_agentemail', 'populate_post_agent_email');
function populate_post_agent_email($value){

global $post;

$agents_email = get_post_meta($post->ID, 'agent_email', true);

return $agents_email;
}

我已将参数名称“agentemail”添加到重力表单字段。如果有人知道我缺少什么能够将此字段(或代理自定义帖子中的任何字段)放入此表单中,将不胜感激。

谢谢。

【问题讨论】:

    标签: php custom-post-type custom-fields gravity-forms-plugin


    【解决方案1】:

    我现在正在处理这个问题 - 我能够将值从一个页面传递到另一个页面,并将信息附加到 url 的末尾 -

    例如。 http://www.your-website.com/?agentemail=agent@email.com

    为此,您必须在编辑相关字段时选中“允许有条件地填充此字段”。

    这对我来说不是万能的(我想在页面加载时生成它,而不是将其附加到按钮上,但这是一个开始。我会在过滤后再次发表评论解决了。​​

    亚当

    【讨论】:

    • 感谢 Adam,我也遇到了这种方法,但不幸的是它不适合我的需要,我还需要它来填充页面加载。如果您发现其他问题,期待您的回音。
    【解决方案2】:

    以下是我使用 Joshua David Nelson 创建的一小段代码填充 GravityForms 下拉列表的方法,josh@joshuadnelson.com

    通过一些小的修改,我能够在下拉框中得到正确的输出(正在寻找用户电子邮件地址而不是用户昵称,但是您可以修改此脚本以输出任何您想要的内容,只需对查询参数)

    // Gravity Forms User Populate, update the '1' to the ID of your form
    add_filter( 'gform_pre_render_1', 'populate_user_email_list' );
    function populate_user_email_list( $form ){
    
    // Add filter to fields, populate the list
    foreach( $form['fields'] as &$field ) {
    
    // If the field is not a dropdown and not the specific class, move onto the next one
    // This acts as a quick means to filter arguments until we find the one we want
        if( $field['type'] !== 'select' || strpos($field['cssClass'], 'your-field-class') === false )
            continue;
    
    // The first, "select" option
        $choices = array( array( 'text' => 'Just Send it to the Default Email', 'value' => 'me@mysite.com' ) );
    
    // Collect user information
    // prepare arguments
    $args  = array(
        // order results by user_nicename
        'orderby' => 'user_email',
        // Return the fields we desire
        'fields'  => array( 'id', 'display_name', 'user_email' ),
    );
    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query( $args );
    // Get the results
    $users = $wp_user_query->get_results();
    //print_r( $users );
    
    // Check for results
        if ( !empty( $users ) ) {
        foreach ( $users as $user ){
            // Make sure the user has an email address, safeguard against users can be imported without email addresses
            // Also, make sure the user is at least able to edit posts (i.e., not a subscriber). Look at: http://codex.wordpress.org/Roles_and_Capabilities for more ideas
            if( !empty( $user->user_email ) && user_can( $user->id, 'edit_posts' ) ) {
                // add users to select options
                $choices[] = array(
                    'text'  => $user->user_email,
                    'value' => $user->id,
                );
            }
        }
    }
    
        $field['choices'] = $choices;
    
    }
    
    return $form;
    }
    
    /* end of populate advisors for dropdown field */
    

    要使其正常工作,您只需将上述代码添加到您的 functions.php 文件中,添加您要更改的 GravityForm 的“ID”(到 add_filter 引用中)并添加您的下拉字段的“类”(其中显示“您的字段类”)。

    如果您对上述代码有任何疑问,请告诉我。

    亚当

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-12
      • 2015-11-04
      • 2011-08-11
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多