Ask your WordPress questions! Pay money and get answers fast! (more info)

Gravity Forms + Carry Stored Meta Value To Dropdown Field WordPress

  • SOLVED

Hi,

I have one issue I just cannot get to work for me. I am not that knowledgeable in GF and had someone work with me on this who left me stranded at this last phase.

Here is what I have.

1) A form that gets submitted - this form includes several dropdown fields that are pre-populated with all the registered website users via the functions file and a pre_render function

2) The form uses a standard dropdown field for these dropdown fields, but then we have a script that takes the selected value and saves it to a hidden custom field so I do have a post meta value in the database (the form gets saved as a custom post type)

3) I have a duplicate of the form on the front end for editing purposes....all the fields are populated fine except for the dropdowns.....which of course just show the pre_render function

I have included below the code that is populating those fields.....id 14 is the original form, id 25 is the UPDATE version of the form......

Of course I understand I am telling the ID 25 to pull in the user list......which I still want it to do if NO OTHER VALUE was selected on the original submission - in the event they want to add one.

So I am guessing I need some type of "else if" statement......if there is a value...pull "custom_field_1" else add all the users......

Can anyone please help....this was supposed to be done last Friday - i kind of got screwed.

THANK YOU!!!!

add_filter( 'gform_pre_render_14', 'populate_user_email_list' );
add_filter( 'gform_pre_render_25', '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'], 'copresenter') === false )
continue;

// The first, "select" option
$choices = array( array( 'text' => 'Choose A Co-Presenter' ) );

// Collect user information
// prepare arguments

$args = array(


// order results by user_nicename
'orderby' => 'display_name',


// 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, 'read' ) ) {

// add users to select options
$choices[] = array(
'text' => $user->display_name,
'value' => $user->id,
);
} }
}
$field['choices'] = $choices;
}
return $form;
}


Answers (2)

2015-08-04

Arnav Joy answers:

I can help you with this , I have done this before.

2015-08-04

Andrea P answers:

I'm not sure if it's me not understanding the situation, but I don't see why would you add the filter to pre-populate to the updating form as well..

if you don't add the filter to it, it will just display what there is in the actual post that you are editing.


2dogs comments:

I did remove the pre-populate from that form as well -- and it still did not bring the saved meta value in. But I do need that filter on the form they can update as well since they can choose to either change or add other users.......which is why I think I need an if/else statement.


Andrea P comments:

the structure you have sounds a bit complicated..
can I see any actual page where these forms are?

which is the name of the meta field where you store the data?


Andrea P comments:

also, how do you build up the form to update the post?
is it placed within the actual post page (like below the current content) or where else?