Support

Account

Forum Replies Created

  • 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.

  • I realize this is nearly a year later, but this is how I set a default.

    The site I am working on has several custom post types that are being used for several things, three of which are “blogs”. There is another that is a ‘widget-post’ that is used for repeating items in different places, and it’s one of these I needed to include by default in my blogs.

    I added a custom field to the widget-posts that allows one to be flagged as a default. I then set up the below function that will insert meta data via ‘save_post’, which is called upon when creating a new post. It will also retroactively save items that are edited.

    
    /**
     * Function will set the default Editorial Team description post if it isn't set at all
     * This sets up the default upon creating a post
     *
     * @uses The $blog_post_types array, which has to have all of the blog post types listed in it
     */
    function jp_blog_save_post( $post_id ) {
    
      // get our post types
      global $post_type, $blog_post_types;
    
      // if it doesn't match, exit
      if( ! in_array( $post_type, $blog_post_types ) )
        return;
    
      // get the meta data
      $editorial_staff_post = get_post_meta( $post_id, 'editorial_staff_post', true );
    
      // if it isn't set, set it
      if( $editorial_staff_post == '' ) {
    
        // set up args
        $args = array(
          'post_type'   => 'widget-post',
          'meta_key'    => 'default_editorial_staff_description',
          'meta_value'  => 1,
          );
    
        // get the post(s) that have the checkbox checked
        $wpost = get_posts( $args );
    
        // set it as a default for this post
        update_post_meta( $post_id, 'editorial_staff_post', $wpost[0]->ID );
      }
    }
    add_action( 'save_post', 'jp_blog_save_post' );
    

    Hope this helps anyone else that runs into this. I’m also sure it can be done with multiple choices.

  • 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' );
    
  • I believe that the problem is that everyone is including the plugin differently. Some are in plugins, some are in themes, etc.

    Here’s the code I just used to get the update nag to go away in my theme:

    
    function one_up_stop_acf_update_notifications( $value ) {
    
    	// remove the plugin from the response so that it is not reported
    	unset( $value->response[ trim( get_template_directory(), '/' ) .'/inc/acf/acf.php' ] );
    	return $value;
    }
    add_filter( 'site_transient_update_plugins', 'one_up_stop_acf_update_notifications', 11 );
    

    ACF is located in /mytheme/inc/acf/, so I use get_template_directory() to get the theme (or parent theme) directory, and I make sure to trim off the ‘/’ because it should not be on the beginning of the string. I then unset that, and it works.

    For plugins, you would need to use something like plugin_dir_path

    Here’s a screen shot showing that I need to upgrade ACF, but there are no nag badges:
    Screen shot of ACF with no nag badges

    ===
    Edit:

    Something clicked after I hit submit…

    Here is the ‘response’ part of the object that is being filtered via ‘site_transient_update_plugins’:

    
        [response] => Array
            (
                [Users/j/Documents/Projects/OWD/JP2015/wp/wp-content/themes/one-up/inc/acf/acf.php] => stdClass Object
                    (
                        [slug] => Users
                        [new_version] => 5.2.1
                        [url] => http://www.advancedcustomfields.com/
                        [package] => 
                    )
    
            )
    

    Each key in that array is the path to the plugin/theme that needs to be updated. If you are following the official instructions, then you should set your filter to unset the same thing you’re setting in step 1. This is what I am using:

    
    // 1. customize ACF path
    function my_acf_settings_path( $path ) {
     
        // update path
        $path = get_template_directory() . '/inc/acf/';
        
        // return
        return $path;
    }
    add_filter( 'acf/settings/path', 'my_acf_settings_path' );
    

    And you can see from my original post that is pretty much what I am removing from the response array.

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