Support

Account

Home Forums ACF PRO Advanced Custom Fields language locale location rule?

Helping

Advanced Custom Fields language locale location rule?

  • Hi, as it seems I can’t translate all the ACF fields (for example the placeholders inside fields), I would like to show a different front-end form for each language. How would I go about setting up such a locale-based location rule?

    This is how far I got:

    function acf_location_rules_types($choices) {
    
        $choices['Language']['lang'] = 'Post Language';
        return $choices;
    }
    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    
    function acf_location_rules_values_lang($choices) {
    
    	if(function_exists("pll_languages_list")) {
    		$choices = pll_languages_list();
    	}
        return $choices;
    }
    add_filter('acf/location/rule_values/lang', 'acf_location_rules_values_lang');
    
    function acf_location_rules_match_lang($match, $rule, $options) {
        
        if(function_exists("pll_current_language")) {
    
    	    $current_lang = pll_current_language();
    	    if($current_lang == "pt") {
    	    	$current_lang == 0;
    	    } else {
    	    	$current_lang == 1;
    	    }
    	    $selected_lang = $rule["value"];
    	    print "<pre>";
    	    var_dump($selected_lang);
    	    print "</pre>";
    	    if($rule['operator'] == "==") {
    	    	$match = ($current_lang == $selected_lang);
    	    }
    	    elseif($rule['operator'] == "!=") {
    	    	$match = ($current_lang != $selected_lang);
    	    }
        }
    
        return $match;
    }
    add_filter('acf/location/rule_match/lang', 'acf_location_rules_match_lang', 10, 3);

    Any advice would be greatly appreciated.

    Thanks,
    Kris

  • I can’t really test this because I’m not using the language plugin anywhere but I might be able to help you figure something out.

    What does it not do? What is working?

    One of you’re problems may be the print and var dump lines. The rule match happens in a AJAX call and that could be causing the AJAX request to fail. The best way to get information on what’s happening during an AJAX request is to turn on error reporting and logging in WP and then use the PHP function error_log() to write stuff to the error log, then look at the error log to see what the output is.

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

The topic ‘Advanced Custom Fields language locale location rule?’ is closed to new replies.