Integrated Tech Solutions

How to set the Selection field options in Gravity form using code

By - Admin February 22, 2024

What is Gravity form?

Gravity form is one of the most popular contact form plugin for WordPress websites.

Generally, we can set the choices of dropdown/selection field from the wordpress dashboard.

But in certain cases, we need to set these choices dynamically from a saparate data source.

add_filter('gform_pre_render_3', 'populate_job_posts');


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

if($field->cssClass == 'dynamic_choices'){


        $job_posts = get_posts(array(
            'post_type' => 'job',
            'numberposts' => -1,
            'post_status' => 'publish',
        ));

        $choices = array();

        foreach ($job_posts as $job_post) {
            $choices[] = array('text' => $job_post->post_title, 'value' => $job_post->ID);
        }

        <span class="php_comment">// Update the placeholder text for the dropdown field.</span>
        $field->placeholder = 'Applied For*';
        $field->choices = $choices;
    }
    }

    return $form;
}

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

WordPress or React – How to choose the best Tech stack for your website

Are you still confused in picking the best Tech stack for your website? Choosing the right tech stack for your...

Know More »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »