Skip Navigation

[Resolved] Is it possible to import data for custom select field options

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to import the options for a custom select field

Solution: First create two options manually. Then use Toolset Export to export all your custom fields. Open the zip file and modify the XML file to include the desired custom fields. Each option tag should have a unique name. See the format below.

<field>
  <id>my-select</id>
  <slug>my-select</slug>
  <type>select</type>
  <name>my-select</name>
  <description></description>
  <data>
    <options>
      <wpcf-fields-select-option-f0ad8e37f8db21b2b56e09589bc4610b-1>
          <title>Option title 1</title>
          <value>1</value>
      </wpcf-fields-select-option-f0ad8e37f8db21b2b56e09589bc4610b-1>
      <wpcf-fields-select-option-b3ae68667fc8b5bdf4049bcf0b5cea72-1>
          <title>Option title 2</title>
          <value>2</value>
      </wpcf-fields-select-option-b3ae68667fc8b5bdf4049bcf0b5cea72-1>
      <wpcf-fields-select-option-my-option-name>
        <title>My Option Name</title>
        <value>myoptionvalue</value>
      </wpcf-fields-select-option-my-option-name>
      <wpcf-fields-select-option-my-next-option-name>
        <title>My Next Option Name</title>
        <value>mynextoptionvalue</value>
      </wpcf-fields-select-option-my-next-option-name>
      <default>no-default</default>
    </options>
    <submit-key>select-650458854</submit-key>
    <disabled_by_type>0</disabled_by_type>
  </data>
  <meta_key>wpcf-my-select</meta_key>
  <meta_type>postmeta</meta_type>
  <wpml_action>2</wpml_action>
  <__types_id>my-select</__types_id>
  <__types_title>my-select</__types_title>
  <hash>c983de0b2103b034786aa5c37b33328f</hash>
  <checksum>c983de0b2103b034786aa5c37b33328f</checksum>
</field>

Verify the XML file is valid using a service like validator.w3.org. Then, zip up the new XML file and the PHP file (no edits are required in the PHP file) into a new archive. Note that the two files should be zipped directly, not added to a directory first. Use Toolset Import to import the archive with your new options in place.

Relevant Documentation: https://toolset.com/documentation/user-guides/how-to-importexport-types-structures/

This support ticket is created 7 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by johanW-2 7 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#522481

I have a Select field in Types that should have about hundred options. Is it possible to import the options or even better connect the field to a external file?

#522595

Hi, there's currently not a way to connect an external, dynamic data source. However, there's a way to import these options. Here's how to do that:
- Add a couple of options to your select field manually
- Use the Toolset export function to export your Types configurations. This will create a zip file you can download
- Unzip the archive and edit the settings.xml file
- Find your select field by searching for the slug. You should find a field block like this:

<field>
            <id>my-select</id>
            <slug>my-select</slug>
            <type>select</type>
            <name>my-select</name>
            <description></description>
            <data>
                <options>
                    <wpcf-fields-select-option-f0ad8e37f8db21b2b56e09589bc4610b-1>
                        <title>Option title 1</title>
                        <value>1</value>
                    </wpcf-fields-select-option-f0ad8e37f8db21b2b56e09589bc4610b-1>
                    <wpcf-fields-select-option-b3ae68667fc8b5bdf4049bcf0b5cea72-1>
                        <title>Option title 2</title>
                        <value>2</value>
                    </wpcf-fields-select-option-b3ae68667fc8b5bdf4049bcf0b5cea72-1>
                    <default>no-default</default>
                </options>
                <submit-key>select-650458854</submit-key>
                <disabled_by_type>0</disabled_by_type>
            </data>
            <meta_key>wpcf-my-select</meta_key>
            <meta_type>postmeta</meta_type>
            <wpml_action>2</wpml_action>
            <__types_id>my-select</__types_id>
            <__types_title>my-select</__types_title>
            <hash>c983de0b2103b034786aa5c37b33328f</hash>
            <checksum>c983de0b2103b034786aa5c37b33328f</checksum>
        </field>

- In the options tag, add a wpcf-fields-select-option- tag for each option. You don't need to use the long random text string for your new items - any unique name will work. For example:

<wpcf-fields-select-option-my-option-name>
  <title>My Option Name</title>
  <value>myoptionvalue</value>
</wpcf-fields-select-option-my-option-name>
<wpcf-fields-select-option-my-next-option-name>
  <title>My Next Option Name</title>
  <value>mynextoptionvalue</value>
</wpcf-fields-select-option-my-next-option-name>

Save the file, then, rezip the updated XML file with the settings.php file from your original zip archive. Import the new zip file using Toolset import.

#522920

I tried this but I get an error "Data to import not set or not valid"
I tried both with the zip file and with the xml file. None of them works.

I edited the file in Sublime text.

Could it be some problem with the encoding?

#523117
Screen Shot 2017-05-11 at 11.10.15 AM.png

My first guess would be a problem with the syntax generated in your XML file. As a first test, I would use an online validator service like validator.w3.org to test your XML file. Click the tab to validate a direct upload, copy + paste the entire contents of your XML file into the validator, then click "Check". You should be shown a message telling you whether or not the document is valid. Screenshot attached (2 warnings, no errors)

If the document is invalid, I may be able to help you fix it if you let me know the error details. If the document is valid, then it's probably an issue with the Zip archive encoding. I could investigate if you post a link where I can download your Zip from an online filesharing service like Dropbox or Drive. I'll enable private reply fields again so you can share securely.

#523957

Hi, it seems that the two files were zipped up into a directory, then the directory was compressed into an archive. Instead, you should compress the files directly into an archive. I did this with the two files from your archive and the new zip worked as expected. I've uploaded that to Dropbox for you to download:
hidden link

Please let me know if this works for you.

#524088

Hi

Now it works perfekt. Thanks for your help.