Populate a Toolset Select field with Custom Options

Toolset offers a Select Field to be populated with values. That’s great if you add a few values. But what if you need let’s say all countries of the world, or all provinces of a country, etc?
Adding them manually to the select Field is one option.

The other, is to craft a small PHP Filter, reading a JSON (or anything you like) file where the data is stored you want to appear in the Select Field as options, and populate that Select Field on the fly with this PHP solution.

It works both in the backend (WP Admin post edit areas) and Front end (CRED Forms).

To implement it, you need a dataset of what you want as options. For example, a JSON files with Country Names and Values.
Then you read that file, json_decode it into an Array of Objects, and populate the Select Field with this.

Here’s the code. The example creates a select Field for all provinces of the Country Spain, using this JSON file. But you can really use anything you like, you will just need to make sure that your PHP Code handles the data you feed it properly.

  1. Download the data (in our case this is the above linked JSON file) and store it on your server (for example, in a Plugin folder, or wherever you like)
  2. Craft your PHP code to read that file (depending where you stored it, you might need to amend the code):
	 * Add Spanish Provinces dynamically to a Toolset Select Field
	 * @param array  $options The array of field options.
	 * @param string $title       The Name of the field.
	 * @param string $type      The Field type (select).
	function populate_select_provinces( $options, $title, $type ) {
		switch ( $title ) {
			case 'Our Select Field Name':
				$options     = array();
				$provinces = wp_json_file_decode( plugin_dir_path( __DIR__ ) . 'path/to/your/file.json', array( 'associative' => false ) );
				foreach ( $provinces as $province ) {
					$options[] = array(
						'#value' => $province->id, // Access the Object's "id" property as option value.
						'#title' => $province->nm, // Access the Object's "nm" property as option name.

		return $options;

  1. Now hook the above code to Toolset’s Filter for the select field options. If you want this code to work both in the WP Admin Post edit area AND in the CRED forms where you inserted this field, you MUST hook the code to BOTH back and frontend. I mention this because many plugins separate concerns and load files with code only in is_admin() or !is_admin() depending where the code is intended to work. So if you do such separation of concern, make sure to load above code globally and not just back or front end, if you want the code to work on both WP Admin post edit and CRED Forms. Hook it like so:
    add_filter( 'wpt_field_options', 'populate_select_provinces', 10, 3 );

That’s it. Now, when you edit your posts where the select Field is included, you’ll see all options from the JSON file added to the select field ready for you to use.