Support

Account

Home Forums Backend Issues (wp-admin) View fields registered via php in the admin Reply To: View fields registered via php in the admin

  • Marko,

    The solution I eventually came up with was to export my base set of fields as a json file. In my plugin I can then import that set of json fields, then edit them in the admin at will. Here is a chunk of my code that you may find useful:

    
    <?php
    
    	function update_fields_from_json(){
    		// Find local JSON directory named 'fields'
    		$dir = new DirectoryIterator( dirname(__FILE__)  . '/fields' );
    		
            // there should only be one json file in the fields folder (in my case)
    		foreach( $dir as $file ) {
    			
    			if ( !$file->isDot() && 'json' == $file->getExtension() ) {
    	
    				$json = json_decode( file_get_contents( $file->getPathname() ), true );
    				
    				// if importing an auto-json, wrap field group in array
    				if( isset($json['key']) ) {
    					$json = array( $json );
    				}
    				
    				foreach( $json as $field_group ) {
                        // group key is in the json file
    					$existing_fields = _acf_get_field_group_by_key("group_57796a3bdfb15");
    				
    					// if the fields don't exist in the db import them now
    					if(!$existing_fields){
    						new jvp_acf_admin_notice('Adding All JVP ACF Fields');
    						
    						acf_import_field_group($field_group);
    					}
                    }
                }
            }
        }
        
    ?>