Support

Account

Home Forums General Issues Custom field configuration is missing with PHP import Reply To: Custom field configuration is missing with PHP import

  • Okay, I came up with my own solution here. It’s very similar to the PHP export solution but just uses the same function in ACF that the JSON import uses. The code for the most part is just copied from the JSON import tool. In order to call this, I just created a special page that executes the code as long as the user is an admin.

    class AcfInit
    {
      public static function install()
      {
        $json = self::jsonExport();
    
        $json = json_decode($json, true);
    
        // Check if empty.
        if( !$json || !is_array($json) ) {
          echo "Invalid data";
          return;
        }
    
        // Ensure $json is an array of groups.
        if( isset($json['key']) ) {
          $json = array( $json );
        }
    
        // Loop over json
        foreach( $json as $field_group ) {
          // Search database for existing field group.
          $post = acf_get_field_group_post( $field_group['key'] );
          if( $post ) {
            $field_group['ID'] = $post->ID;
          }
    
          // Import field group.
          acf_import_field_group( $field_group );
        }
    
        echo "done!";
      }
    
      public static function jsonExport()
      {
        return 'JSON EXPORT CONTENTS GO HERE';
      }
    }