Skip to content

Instantly share code, notes, and snippets.

@francescocarlucci
Last active December 1, 2019 17:27
Show Gist options
  • Save francescocarlucci/e7377937ee831200e52733fc1f449e5a to your computer and use it in GitHub Desktop.
Save francescocarlucci/e7377937ee831200e52733fc1f449e5a to your computer and use it in GitHub Desktop.
add_filter('ninja_forms_render_options','ninja_forms_pre_population_callback', 10, 2);
function ninja_forms_pre_population_callback($options, $settings) {
// target only the field with this key
if( $settings['key'] == 'my_field_key' ) {
// write the query to fetch the data
$args = array(
'post_type' => 'post',
'post_status' => 'publish'
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) {
$options = array();
while ( $posts->have_posts() ) {
$posts->the_post();
// $options is the variable which contains tha values rendered
// we will use the post title as lable and the ID as value
$options[] = array(
'label' => get_the_title(),
'value' => get_the_ID()
);
} // endwhile
} // endif
wp_reset_postdata();
}
return $options;
}
@Shritama33
Copy link

Hi Francesco,

 I have a Google Sheet that has a list of labels. I am trying to use Google API to get this list and store it in the $options variable. Is there a way to save multiple Labels in the options variable? Currently I can see that we can only add a single label through this variable.

@francescocarlucci
Copy link
Author

Hi Francesco,

 I have a Google Sheet that has a list of labels. I am trying to use Google API to get this list and store it in the $options variable. Is there a way to save multiple Labels in the options variable? Currently I can see that we can only add a single label through this variable.

$options variable is already an array, so you can store in it more values in a key => value format ;)

@Shritama33
Copy link

Hi Francesco,

 I have a Google Sheet that has a list of labels. I am trying to use Google API to get this list and store it in the $options variable. Is there a way to save multiple Labels in the options variable? Currently I can see that we can only add a single label through this variable.

$options variable is already an array, so you can store in it more values in a key => value format ;)

Okay thank you very much. I figured it out.

@Shritama33
Copy link

So I have two select fields in my Ninja Form. Please note that my google sheet is a list of products and corresponding brands. First one displays a list of brands from the google sheet and the second one needs to display the products based on the brand selected. I have the Conditional Logic add-on installed but I am not sure how to get the selected brand in the functions.php file without the need to submit the form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment