1

Tried to be as specific as possible in the title. Essentially I have a real estate site that I am working on that has property listings. The property listing is a custom post type and there are several other custom post types (each with their own custom fields) attached to it.

Each property has an agent (custom post type) attached to it with custom fields such as email, facebook, phone number, etc.

I need to dynamically pre-populate a hidden field on Gravity Forms with the agent email (which is NOT the same as the author email) so that I can send an email to that address.

I have tried the following with no luck because I'm sure something is missing to call the custom field from the Agent custom post type, but I'm not sure how. This is what I'm working with so far:

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;
}

I have added the parameter name "agentemail" to the gravity form field. If anyone knows what I am missing to be able to get this field (or any field from the agent custom post) into this form, it would be much appreciated.

Thanks.

2 Answers 2

1

Here's How I Populated my GravityForms Dropdown using a little code that I found created by Joshua David Nelson, [email protected]

With a few minor modifications I was able to get the proper output to the dropdown box (was looking for user email addresses instead of user nicenames, but you can modify this script to output anything you want with a few small changes to the query args)

// 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' => '[email protected]' ) );

// 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 */

To get this to work all you should need to do is add the above code to your functions.php file, add the 'ID' (into the add_filter reference) of the GravityForm that you're looking to alter and add the 'Class' of your dropdown field (where is says 'your-field-class').

If you have any questions about the above code, let me know.

Adam

Sign up to request clarification or add additional context in comments.

Comments

0

I'm working on this one right now - I was able to pass the values from one page to another appending the information onto the end of the url -

ex. http://www.your-website.com/[email protected]

For this to work you have to check the 'Allow this field to be populated Conditionally' when editing the field in question.

It's not the be-all-end-all for me (I'd like to generate this on page load, rather than attaching it to a button, but it's a start. I'll comment again when I have the filtering worked out.

Adam

1 Comment

Appreciate that Adam, I came across that method as well but unfortunately it doesn't suit my needs and I also need it to populate on page load. Look forward to hearing back if you figure something else out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.