Support

Account

Forum Replies Created

  • If works!!! 🙂
    Thank you very much! Here’s my final code in case it interests someone:

    
    <?php
    
    /**
     * Class Henri_ACF_SyncOptions
     *
     * This class sync the selected options fields between all active languages.
     *
     */
    class Henri_ACF_SyncOptions {
    
    	private static $inited = false;
    
    	/**
    	 * @var array
    	 * List fields to update
    	 */
    	private static $henri_fields_to_sync = array(
    		"field_group_henri_settings_general_analytics_henri_google_analytics",
    		"field_group_henri_settings_general_medias_henri_remove_accent",
    		"field_group_henri_settings_general_access_disable_update",
    		"field_group_henri_settings_general_access_password_protect",
    		"field_group_henri_settings_general_social_henri_social_icons_group",
    	);
    
    	public static function init() {
    		if ( self::$inited ) {
    			return;
    		}
    		self::$inited = true;
    		add_action( 'acf/save_post', array( 'Henri_ACF_SyncOptions', 'onPostSave' ), 10 );
    	}
    
    	public static function onPostSave() {
    		global $sitepress;
    		if ( ! isset( $sitepress ) ) {
    			return;
    		}
    		// array of field values
    		$postFields  = $_POST['acf'];
    		$activeLangs = $sitepress->get_ls_languages();
    		foreach ( self::$henri_fields_to_sync as $fieldToSync ):
    			if ( isset( $postFields[ $fieldToSync ] ) ):
    				foreach ( $activeLangs as $key => $value ) {
    					if ( $key !== ICL_LANGUAGE_CODE ) {
    						set_global_option_icl( $fieldToSync, $postFields[ $fieldToSync ], $key );
    					}
    				}
    			endif;
    		endforeach;
    	}
    
    }
    if(is_admin()):
    	Henri_ACF_SyncOptions::init();
    endif;
    
  • Thanks for your answer!!

    What I need is to sync the data of an option page between multiple language, using WPML.
    – In a plugin
    – Option page created via php by the plugin (acf_add_options_page, acf_add_local_field,etc)
    – When the user save the form in any language in the admin, I need the same data to be in the form when the user switch language. These are settings shared for all languages.

    Best approach I found was to watch for the save and then resave the data in all languages. Maybe there’s a better way…

    If not, here’s my code to do it. It works fine for all the fields, except one repeater)

    $henri_fields_to_sync = array(
    		"henri_google_analytics",
    		"henri_remove_accent",
    		"henri_disable_update",
    		"henri_password_protect",
    		"henri_social_icons_group",
    	);
    	if (  isset( $_SESSION['is_saving_acf_henri'] ) ) {
    		unset( $_SESSION['is_saving_acf_henri'] );
    	}
    	/** Sync acf_field between language */
    	function henri_sync_acf_field_language( $fieldValue, $field, $post_id ) {
    		if ( ! isset( $_SESSION ) ) {
    			session_start();
    		}
    		global $sitepress;
    		if ( !isset( $sitepress ) ) {
    			return;
    		}
    		if ( ! isset( $_SESSION['is_saving_acf_henri'] ) ) {
    			$_SESSION['is_saving_acf_henri'] = true;
    			$activeLang          = $sitepress->get_ls_languages();
    			foreach ( $activeLang as $key => $value ) {
    				if ( $key !== ICL_LANGUAGE_CODE ) {
    					set_global_option_icl( $post_id['name'], $fieldValue, $key );
    				}
    			}
    			unset( $_SESSION['is_saving_acf_henri'] );
    		}
    		return $fieldValue;
    	}
    
    	foreach ( $henri_fields_to_sync as $henri_field_to_sync ):
    		add_filter( 'acf/update_value/name=' . $henri_field_to_sync, 'henri_sync_acf_field_language', 10, 3 );
    	endforeach;

    Thanks again for your time!

    stef

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