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

Gravity form - populate drop down select based on page URL WordPress

  • SOLVED

I'd like to populate / pre-select a value based on the URL of the page.

Eg;

Drop down select:

Page 01
Page 02
Page 03

URLs:

www.example.com/page01
www.example.com/page02
www.example.com/page03

If the form is being viewed on www.example.com/page02 then 'Page 02' will be pre-selected.

Answers (3)

2017-06-04

Reigel Gallarde answers:

do you still need help on this? how are you creating your dropdown?


Austin comments:

In Gravity Forms.


Reigel Gallarde comments:

so you have a already a dropdown on the page that has options?

sorry because the question title and the description aren't together... the question title says populate, but the description kinda says you just want to have this certain option be selected when you are at a certain page.


Austin comments:

Ah yes, you're right.

The drop down in the form already has options.

I want to pre-select an option based on the URL of the page. Also, the page will sometimes have UTM parameters.


Reigel Gallarde comments:

I have a few questions before I can give an answer... I want to know how you are adding the options...
Are you manually entering the Choices or is it dynamically populated?


Austin comments:

I have manually entered them. I will continue to add more options over time.


Reigel Gallarde comments:

So I have here my code: https://gist.github.com/reigelgallarde/a5d360c084e895c71cc34df7c3b4c82f

In my code, you need to add a Custom CSS Class in Appearance tab. The class is "my-select" you can use your own on this. Just make sure your Custom CSS Class is the same in the code.

Next, I'm using $post->post_name to check the values. that is the slug of the post/page. In this example, www.example.com/page01, it's it's page01. Your gForm Dropdown choices should also have this format. Should be case sensitive.

This is kind of not a good way to go though, so I have a suggestion.

If you can, I strongly suggest you do, set the value for each choices. You can do this in General tab, right above choices, you have a checkbox that says "Show Values". Checking this option will allow you to specify a value for each choice. So I suggest you enter in the post id for each choices. Then on my code, use the commented out lines.

Let me know if you have problems.


Reigel Gallarde comments:

I forgot to mention, the form ID... in my code I use 22... like `gform_pre_render_22`. You should replace this with your form ID.


Austin comments:

Hi Reigel - I've tried the code using the slug and the post id.

I changed the form ID number and put 'my-select' in the custom CSS field.

It doesn't have any effect when I test the form.


Reigel Gallarde comments:

do you have a link? or can you help me debug it?

in line above `foreach ( $field->choices as $key => $choice ) {`
can you add print_r($field->choices);
then look at it in the front-end and let me know what you get.


Austin comments:

It doesn't change anything. - i just have the options in the select as normal.


Austin comments:

What's your email? Hopefully we can get it working.


Reigel Gallarde comments:

Ok, I revised my code... again, change 22 accordingly.
https://gist.github.com/reigelgallarde/a5d360c084e895c71cc34df7c3b4c82f
I changed `$current_post_id === $choice['value']` to `$current_post_id == $choice['value']`
Basically just replace what you have with the revised code.

here's a screenshot of my choices
http://imgur.com/QFHiLMg

Custom CSS Class
http://imgur.com/Y7l6KRL


Reigel Gallarde comments:

you can reached me using my contact form here http://reigelgallarde.me/services/


Austin comments:

Hi - I tried the new code but it still didn't work.

I will contact you later today on your website.

Thanks :)


Reigel Gallarde comments:

I really can't further more revised the code... it's already in it's simplest form.
And it works on my test http://imgur.com/On3b86A

Not sure what's the problem on your end. I wish I could have the chance to check your website's code.


Reigel Gallarde comments:

are you putting the form on a custom post type?

In any case, can you try create a test form with only select as field.
Then put the form on a test page. Should put the the ID of the the test page on the value of one of the choices of the select. put my-select as Custom CSS Class.

Then change the form ID of the code to reflect the test form.

Here's a demo I created. Using the current code I gave you.

http://demo.reigelgallarde.me/ppqp/page-01/
http://demo.reigelgallarde.me/ppqp/page-02/
http://demo.reigelgallarde.me/ppqp/page-03/
http://demo.reigelgallarde.me/ppqp/sample-page/


Reigel Gallarde comments:

choices http://imgur.com/X6w2oIm


Austin comments:

Got it working - thanks!

The form was in a modal popup that had its own ID - it worked when I used that.

I'll paste the code here in case anyone else needs it:

add_filter( 'gform_pre_render_22', 'gform_default_selected' );
add_filter( 'gform_pre_validation_22', 'gform_default_selected' );
add_filter( 'gform_pre_submission_filter_22', 'gform_default_selected' );
add_filter( 'gform_admin_pre_render_22', 'gform_default_selected' );

function gform_default_selected( $form ) {
global $post;

if (is_admin() ) return $form;

$current_post_id = $post->ID;

foreach ( $form['fields'] as &$field ) {

if ( $field->type != 'select' || strpos( $field->cssClass, 'my-select' ) === false ) {
continue;
}


foreach ( $field->choices as $key => $choice ) {
$field->choices[$key]['isSelected'] = ( $current_post_id == $choice['value'] );
}

}

return $form;
}

2017-06-04

Sébastien | French WordpressDesigner answers:

Paste this code in your file functions.php :
[code]

<?php

add_filter( 'gform_pre_render_51', 'populate_posts' );
add_filter( 'gform_pre_validation_51', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_51', 'populate_posts' );
add_filter( 'gform_admin_pre_render_51', 'populate_posts' );

function populate_posts( $form ) {

global $post;
$current_post = $post->post_name;

foreach ( $form['fields'] as &$field ) {

if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}

// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$posts = get_posts( 'numberposts=-1&post_status=publish' );

$choices = array();

foreach ( $posts as $post ) {
if ($current_post === $post->post_name) {
$choices[] = array( 'text' => $post->post_title, 'value' => sanitize_title($post->post_title), 'isSelected' => true );
} else {
$choices[] = array( 'text' => $post->post_title, 'value' => sanitize_title($post->post_title) );
}
}

// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;

}

return $form;
}

[/code]


Sébastien | French WordpressDesigner comments:

You need to customize the line
$posts = get_posts( 'numberposts=-1&post_status=publish' );

In your case you can replace by
$posts = get_posts( 'post_type=page&numberposts=-1&post_status=publish' );
or
$posts = array(
'page 01' => 'www.example.com/page01',
'page 02' => 'www.example.com/page02',
'page 03' => 'www.example.com/page03',
'page 04' => 'www.example.com/page04',);


Austin comments:

Hi - what do I call my drop-down select field?

I've tried Select a Post and used:

$posts = array(
'page 01' => 'www.mydomainhere.com/webinar-sts-06-15-17/',
'page 02' => 'www.mydomainhere.com/webinar-sdl-06-20-17/',
);

It doesn't change the value when I load the second page.


Sébastien | French WordpressDesigner comments:

you need to use the exact title and the exact URL
Your array will be :


$posts = array(
'page 01' => 'http://www.mydomainhere.com/webinar-sts-06-15-17/',
'page 02' => 'http://www.mydomainhere.com/webinar-sdl-06-20-17/',
);


Austin comments:

Hi - what do mean by exact title?

And what do I call my field?


Austin comments:

You understand that I need to pre-select the value based on the URL of the page?


Sébastien | French WordpressDesigner comments:

we only want to modify fields that are select (drop down) fields AND have a CSS class titled "populate-posts".


Austin comments:

It's not doing anything yet.

I have a drop down field created:

Label: My Page

Custom CSS: populate-posts

values:

page 01
page 02

And in functions.php

$posts = array(
'page 01' => 'http://www.mydomain.com/webinar-sts-06-15-17/',
'page 02' => 'http://www.mydomain/webinar-sdl-06-20-17/',
);


Sébastien | French WordpressDesigner comments:

yes I understand :)

If you use
$posts = array(
'page 01' => 'http://www.mydomainhere.com/webinar-sts-06-15-17/',
'page 02' => 'http://www.mydomainhere.com/webinar-sdl-06-20-17/',
);

you need to replace this code

$choices = array();

foreach ( $posts as $post ) {
if ($current_post === $post->post_name) {
$choices[] = array( 'text' => $post->post_title, 'value' => sanitize_title($post->post_title), 'isSelected' => true );
} else {
$choices[] = array( 'text' => $post->post_title, 'value' => sanitize_title($post->post_title) );
}
}

by this one :


$choices = array();


foreach ( $posts as $k => $v ) {
if ($current_post === $v) {
$choices[] = array( 'text' => $k, 'value' => sanitize_title($k), 'isSelected' => true );
} else {
$choices[] = array( 'text' => $k, 'value' => sanitize_title($k) );
}
}


Austin comments:

Hi - I still can't get this to work.

The nearest I have found to want I need is:

/**
* Pre Select a dropdown choice/option in Gravity Forms
*
* Create a dynamic list of dropdown choices from a custom post type
* Based on the current page, pre-select a choice in the drop down
*
* @var howto_dynamic_field = the dynamic field set in Gravity Forms on the field group under the Advanced tab
* @var howto_post_type = Your custom post type you are listing in the drop down
*
*/
add_filter( 'gform_pre_render_1', 'howto_preselect_dropdown' );
add_filter( 'gform_pre_validation_1', 'howto_preselect_dropdown' );
add_filter( 'gform_pre_submission_filter_1', 'howto_preselect_dropdown' );
add_filter( 'gform_admin_pre_render_1', 'howto_preselect_dropdown' );

function howto_preselect_dropdown( $form ) {

global $post;

$current_post = $post->post_name;

foreach ( $form['fields'] as &$field ) {

if ( $field->type != 'select' || strpos( $field->inputName, 'howto_dynamic_field' ) === false ) {
continue;
}

$args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1
);
$posts = get_posts( $args );

$choices = array();

foreach ( $posts as $post ) {
if ($current_post === $post->post_name) {
$choices[] = array( 'text' => __($post->post_title, 'ozh_reap'), 'value' => sanitize_title($post->post_title), 'isSelected' => true );
} else {
$choices[] = array( 'text' => __($post->post_title, 'ozh_reap'), 'value' => sanitize_title($post->post_title));
}
}

$field->placeholder = 'Select';
$field->choices = $choices;

}

return $form;
}


Sébastien | French WordpressDesigner comments:

and the name of your select is well "howto_dynamic_field" ?


Sébastien | French WordpressDesigner comments:

This is the code you need, but your select must have the class "populate-posts", of course :


<?php

add_filter( 'gform_pre_render_51', 'populate_posts' );
add_filter( 'gform_pre_validation_51', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_51', 'populate_posts' );
add_filter( 'gform_admin_pre_render_51', 'populate_posts' );

function populate_posts( $form ) {

global $post;
$current_post = $post->post_name;

foreach ( $form['fields'] as &$field ) {

if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}

// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1
);
$posts = get_posts( $args );

$choices = array();

foreach ( $posts as $post ) {
if ($current_post === $post->post_name) {
$choices[] = array( 'text' => $post->post_title, 'value' => sanitize_title($post->post_title), 'isSelected' => true );
} else {
$choices[] = array( 'text' => $post->post_title, 'value' => sanitize_title($post->post_title) );
}
}

// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;

}

return $form;
}


Austin comments:

Hi - still doesn't do anything.


Sébastien | French WordpressDesigner comments:

mail me : maildeseb[at]gmail[point]com

2017-06-05

Arnav Joy answers:

Hello Austin,
Are you still looking for help ?
if yes please show me your site and the code you are using so far.
please add me on skype : arnav.joy or mail me at : [email protected]
-Arnav