Integrated Tech Solutions

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

By - Admin, Updated on 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 »

AR in Websites – Top 3 Platforms for Bringing Augmented Reality to Your Web Experience

As technology advances, augmented reality (AR) is no longer limited to gaming or social media apps. Integrating AR into websites...

Know More »

Webhook Explained for Beginners: Everything You Need to Know

In the world of APIs, automation, and modern web applications, the term “webhook” often comes up. But what exactly is...

Know More »