Title: Import custom fields via plugin
Last modified: August 22, 2016

---

# Import custom fields via plugin

 *  Resolved [erikad42](https://wordpress.org/support/users/erikad42/)
 * (@erikad42)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/import-custom-fields-via-plugin/)
 * Thanks for this incredibly useful plugin!
 * I have been reading through everything I can find on how to import custom fields
   via a plugin and so far have not had much luck. I haven’t done a lot of plugin
   development, though, so I’m probably just missing something about how the CFS
   works. Here’s what I’ve done so far:
 * 1. Built out my custom fields in a dev environment and exported them
    2. Added
   them to a text file 3. First activated CFS, then tried to activate my plugin
 * Here’s my code:
 *     ```
       global $cfs;
   
               $content =  file_get_contents( plugin_dir_path( __FILE__ ) . '/custom-meta/custom-meta-fields.txt', true);
   
               $meta = json_decode( $content, true );
   
               $options = array(
                   'import_code' => $meta
               );
   
               $cfs->field_group->import( $options );
       ```
   
 * When I activate the plugin, I get the notice “Trying to get property of non-object”
   on the line $cfs->field_group->import( $options );
 * The JSON is valid and I’m using PHP 5.5. Based on some trial and error, I’m wondering
   if the problem is with trying to access CFS from inside another plugin rather
   than with my exported fields. Any pointers or suggestions would be much appreciated!
 * [https://wordpress.org/plugins/custom-field-suite/](https://wordpress.org/plugins/custom-field-suite/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Matt Gibbs](https://wordpress.org/support/users/mgibbs189/)
 * (@mgibbs189)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/import-custom-fields-via-plugin/#post-5389003)
 * Your code looks pretty spot-on. It’s likely a timing issue. Your plugin is trying
   to use $cfs before it’s been fully loaded. Your two options are
 * 1. Put your code within an `init` hook having priority of ~12
    2.Put your code
   within a `cfs_init` hook
 * Also, this is totally minor, but you can now use `CFS()` instead of the `$cfs`
   variable, e.g.
 *     ```
       CFS()->field_group->import( $options );
       ```
   
 *  Thread Starter [erikad42](https://wordpress.org/support/users/erikad42/)
 * (@erikad42)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/import-custom-fields-via-plugin/#post-5389025)
 * Putting it inside a cfs_init hook worked, it was definitely a timing issue. Thanks
   for the tip about CFS()!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Import custom fields via plugin’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-field-suite.svg)
 * [Custom Field Suite](https://wordpress.org/plugins/custom-field-suite/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-field-suite/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-field-suite/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-field-suite/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-field-suite/reviews/)

## Tags

 * [CFS](https://wordpress.org/support/topic-tag/cfs/)
 * [Import](https://wordpress.org/support/topic-tag/import/)

 * 2 replies
 * 2 participants
 * Last reply from: [erikad42](https://wordpress.org/support/users/erikad42/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/import-custom-fields-via-plugin/#post-5389025)
 * Status: resolved