Support

Account

Home Forums Feature Requests Automatic Synchronized Json

Solving

Automatic Synchronized Json

  • Would be super awesome if the synchronized json (http://www.advancedcustomfields.com/resources/synchronized-json/) functionality ran automatically instead of manually having to “Sync” field groups.

    This means that the JSON becomes the single point of truth if you will.
    Merging is taken care of with GIT / insert other VCS here.

  • I needed this, as well, so I just went into the ACF code to see how the sync itself was done. I took out the code to perform the sync and added it to another function and then hooked it to admin_init.

    Below is the code, but I must encourage everyone to use at your own risk, as it will run the update regardless if it is “approved” by the user or not.

    
    /**
     * Function that will update ACF fields via JSON file update
     */
    function jp_sync_acf_fields() {
    
    	// vars
    	$groups = acf_get_field_groups();
    	$sync 	= array();
    
    	// bail early if no field groups
    	if( empty( $groups ) )
    		return;
    
    	// find JSON field groups which have not yet been imported
    	foreach( $groups as $group ) {
    		
    		// vars
    		$local 		= acf_maybe_get( $group, 'local', false );
    		$modified 	= acf_maybe_get( $group, 'modified', 0 );
    		$private 	= acf_maybe_get( $group, 'private', false );
    		
    		// ignore DB / PHP / private field groups
    		if( $local !== 'json' || $private ) {
    			
    			// do nothing
    			
    		} elseif( ! $group[ 'ID' ] ) {
    			
    			$sync[ $group[ 'key' ] ] = $group;
    			
    		} elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {
    			
    			$sync[ $group[ 'key' ] ]  = $group;
    		}
    	}
    
    	// bail if no sync needed
    	if( empty( $sync ) )
    		return;
    
    	if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {
    		
    		// vars
    		$new_ids = array();
    		
    		foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {
    			
    			// append fields
    			if( acf_have_local_fields( $key ) ) {
    				
    				$sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );
    				
    			}
    			// import
    			$field_group = acf_import_field_group( $sync[ $key ] );
    		}
    	}
    }
    add_action( 'admin_init', 'jp_sync_acf_fields' );
    
    • jgraup

    • August 9, 2016 at 9:56 am

    Is this a feature yet? Can we trigger it from wp-cli?

    
    <?php
    
    if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'ACF_Commands' ) ) :
    
        /**
         * ACF_Commands
         */
        class ACF_Commands extends WP_CLI_Command {
    
            /**
             * Sync ACF Fields
             *
             * ## OPTIONS
             *
             * @when init
             *
             * @example
             *
             *  wp acf sync
             *
             */
            function sync ( $args, $assoc_args ) {
    
                // vars
                $groups = acf_get_field_groups();
                $sync 	= array();
    
                // bail early if no field groups
                if( empty( $groups ) )
                    return;
    
                // find JSON field groups which have not yet been imported
                foreach( $groups as $group ) {
    
                    // vars
                    $local 		= acf_maybe_get( $group, 'local', false );
                    $modified 	= acf_maybe_get( $group, 'modified', 0 );
                    $private 	= acf_maybe_get( $group, 'private', false );
    
                    // ignore DB / PHP / private field groups
                    if( $local !== 'json' || $private ) {
    
                        // do nothing
    
                    } elseif( ! $group[ 'ID' ] ) {
    
                        $sync[ $group[ 'key' ] ] = $group;
    
                    } elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {
    
                        $sync[ $group[ 'key' ] ]  = $group;
                    }
                }
    
                // bail if no sync needed
                if( empty( $sync ) ) {
                    WP_CLI::success( "No ACF Sync Required" );
                    return;
                }
    
                if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {
    
                    // vars
                    $new_ids = array();
    
                    foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {
    
                        // append fields
                        if( acf_have_local_fields( $key ) ) {
    
                            $sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );
    
                        }
                        // import
                        $field_group = acf_import_field_group( $sync[ $key ] );
                    }
                }
    
                WP_CLI::success( 'ACF SYNC SUCCESS!' );
            }
    
        }
    
        WP_CLI::add_command( 'acf', 'ACF_Commands' );
    
    endif; // ACF_Commands
  • Thanks, this works great! This seems to work only with adding fields that didn’t exist before. Is there a way to automatically delete fields in the database that were deleted via their JSON files? I am using this feature on a multisite.

  • Personally, I am not sure, and I haven’t touched ACF in months due to I am no longer developing 🙂

    When you do a manual sync does the process remove fields that have been removed from the JSON file? If not, then there’s probably a reason for it. 99% of the time developers allow the addition of data like this through imports, but not removal of the data. This is due to it is much easier to have users manually delete data then try to recover any data deleted from the database due to a bad import.

  • Thanks for the quick response! When I turn the auto syncing off it doesn’t delete the field on the backend when I run the syncing manually. It did when I used the ACF Pro JSON Sync plugin, but that didn’t have an auto sync feature like this for the syncing feature that is baked into the plugin.

  • Thank you jessepearson, it works great!

    @ACF Team: any chance a function/action like this can get into the plugin? It is very useful for automatic site deployment, especially in multisite installations.

  • “This means that the JSON becomes the single point of truth if you will.”

    I’d love such feature. I’m having issues regularly with databases out of sync, or fields deleted from JSON but still present in database and displaying on pages…

    Getting rid of the database copy of the fields and of the sync system would be ideal for me (but keeping the admin UI for creating / update fields of course).

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.