• Resolved erikad42

    (@erikad42)


    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://ww.wp.xz.cn/plugins/custom-field-suite/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    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

    (@erikad42)

    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.