Support

Account

Home Forums Front-end Issues field choices

Solved

field choices

  • Hello !

    I have some code that allow me to translate ACF instructions and lables with WP Globus :

    
    add_filter( 'acf/get_valid_field', 'filter__wpglobus_get_valid_field' );
    function filter__wpglobus_get_valid_field( $field ) {
    
    	if ( ! class_exists( 'WPGlobus' ) ) {
    		return $field;
    	}
    
    	if ( is_admin() ) {
    		return $field;
    	}
    
    	$_fields[] = 'label';
    	$_fields[] = 'instructions';
    	
    
    	foreach( $_fields as $_field ) {
    		if ( ! empty( $field[ $_field ] ) ) {
    			$field[ $_field ] = WPGlobus_Core::text_filter( $field[ $_field ], WPGlobus::Config()->language );
    		}
    	}
    	
    
    	return $field;
    }
    

    How can I modify my code in order to loop through the choices of my field to allow the translation of my choices ?

    I also have a repeater field, but I cannot get access to the subfields.

    Thank you for helping,

    Marc

  • First, why are you using acf/get_valid_field, you can probably use acf/load_field and just do a check to make sure you’re not on the acf field group edit page. This would likely solve your problem with sub fields. https://www.advancedcustomfields.com/resources/acfload_field/

    for the choices it would probably look something like

    
    foreach ($field['choices'] as $value => $label)) {
      $field['choices'][$value] = WPGlobus_Core::text_filter($label, WPGlobus::Config()->language );
    }
    
  • Hi John,

    Thank you. I’ve tried the code in acf/load_field but, I receive an error message : Invalid argument supplied for foreach()

    
    foreach ($field['choices'] as $value => $label) {
      $field['choices'][$value] = WPGlobus_Core::text_filter($label, WPGlobus::Config()->language );
    }
    

    any idea ?

  • I guess that depends on what type of field you’re trying to change the choice labels on. Since you are running this on every field you need to check that it’s a field type that actually has choices, or make sure that choices is set.

    
    if (!empty($field['choices'])) {
      foreach ($field['choices'] as $value => $label) {
        $field['choices'][$value] = WPGlobus_Core::text_filter($label,   WPGlobus::Config()->language );
      }
    }
    
  • Thank you John !
    🙂

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

The topic ‘field choices’ is closed to new replies.