使用自定义帖子类型中的自定义字段动态预填充Gravity Forms字段


Dynamically pre-populate Gravity Forms field with custom fields from custom post types

试图在标题中尽可能具体。本质上,我有一个房地产网站,我正在工作,有房地产清单。属性列表是一个自定义帖子类型,还有其他几个自定义帖子类型(每个类型都有自己的自定义字段)。

每个房产都有一个代理(自定义帖子类型),带有自定义字段,如电子邮件、facebook、电话号码等。

我需要用代理电子邮件(与作者电子邮件不同)动态地预先填充Gravity Forms上的隐藏字段,这样我就可以向该地址发送电子邮件。

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

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"添加到重力表单字段中。如果有人知道我缺少什么,能够将此字段(或代理自定义帖子中的任何字段)输入此表单,将不胜感激。

谢谢。

以下是我如何使用Joshua David Nelson创建的小代码填充GravityForms Dropdown,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引用中)添加到下拉字段的"Class"(其中is表示"您的字段Class")。

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

Adam

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

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

要做到这一点,您必须在编辑有问题的字段时选中"允许有条件地填充此字段"。

对我来说,这并不是全部(我想在页面加载时生成这个,而不是将它附加到按钮上,但这只是一个开始。当我完成过滤后,我会再次发表评论。

Adam