Skip to content
Advertisement

How to dynamically pass an array of values to a field in Contact Form 7?

According to the official Contact Form 7 docs, it is possible to pass a a default value to CF7 from the shortcode, in this way:

JavaScript
JavaScript
JavaScript

This works for a simple text field, but I need to pass an array of values to a <select> field and use them as <option>s inside it; I’ve tried to modify a bit this code, but apparently it isn’t working, or at least I’ve not been able to.

Is it possible to use the shortcode to send dynamic data to CF7 even if not a single plain text like this?

If not, I’m open to every other kind of solution, even if it involves another method, or some additional plugin; is there some other way to dynamically send an array of values to <select> fields in Contact Form 7?

These values are data queried from the database (such as post names, custom fields, and so on), so they need to come from php first even if there is a solution that involves javascript.

Advertisement

Answer

Here’s an example of a form tag I’ve used for getting US States. This is a <select> generated from an array. This is probably more along the lines of what you want to do.

You can see that I also use the usermeta billing_state to pre-select the choice.

With that said, you should also be able to use this same method to create a select tag that performs any WP_Query and puts the results into an option tag.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement