WPML Current Language Choices

With WPML you can create multiple versions of posts each with their own language. When populating posts with Populate Anything, there’s no way to identify the current language and filter by that language with the existing UI. This snippet automatically filters posts by the current language.

Credit: Dennis Hunink

Instructions

  1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
  2. Add the CSS class wpml-limit-to-current-language to any GPPA-enabled field you want filtered.
  3. Populate that field with posts; the snippet filters choices to the current WPML language.

Code

Filename: gppa-wpml-current-language-choices.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
 * Gravity Perks // Populate Anything // WPML Current Language Choices
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * With WPML you can create multiple versions of posts each with their own language.
 * When populating posts with Populate Anything, there's no way to identify the current
 * language and filter by that language with the existing UI. This snippet automatically
 * filters posts by the current language.
 *
 * Credit: Dennis Hunink
 *
 * Instructions:
 * 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 * 2. Add the CSS class `wpml-limit-to-current-language` to any GPPA-enabled field you want filtered.
 * 3. Populate that field with posts; the snippet filters choices to the current WPML language.
 */
add_filter( 'gppa_input_choices', 'hb_wpml_limit_gppa_choices_to_current_language', 10, 4 );

function hb_wpml_limit_gppa_choices_to_current_language( $choices, $field, $objects, $field_values ) {
	// Ensure WPML is active.
	if ( ! function_exists( 'icl_object_id' ) ) {
		return $choices;
	}

	// Only apply filtering if the marker CSS class is present.
	$css_class = isset( $field->cssClass ) ? $field->cssClass : '';
	if ( strpos( $css_class, 'wpml-limit-to-current-language' ) === false ) {
		return $choices;
	}

	// Get current WPML language (e.g. 'nl', 'en').
	$current_lang = apply_filters( 'wpml_current_language', null );
	if ( ! $current_lang ) {
		return $choices;
	}

	$filtered_choices = array();

	foreach ( $choices as $index => $choice ) {
		$post_id   = 0;
		$post_type = null;

		// Preferred: WP_Post object from $objects array.
		if ( isset( $objects[ $index ] ) && $objects[ $index ] instanceof WP_Post ) {
			$post_id   = $objects[ $index ]->ID;
			$post_type = $objects[ $index ]->post_type;
		}

		// Fallback: use the choice value as post ID.
		if ( ! $post_id && isset( $choice['value'] ) ) {
			$post_id = absint( $choice['value'] );

			if ( $post_id && ! $post_type ) {
				$post_obj  = get_post( $post_id );
				$post_type = $post_obj ? $post_obj->post_type : null;
			}
		}

		// If we can resolve a post and post type, apply WPML language check.
		if ( $post_id && $post_type ) {
			// Convert the post type to a WPML element type, e.g. 'post_post'.
			$element_type = apply_filters( 'wpml_element_type', $post_type );

			// Get the language code for this specific element.
			$choice_lang = apply_filters(
				'wpml_element_language_code',
				null,
				array(
					'element_id'   => $post_id,
					'element_type' => $element_type,
				)
			);

			// If WPML knows the language and it does not match the current language, skip this choice.
			if ( $choice_lang && $choice_lang !== $current_lang ) {
				continue;
			}
		}

		// Keep the choice.
		$filtered_choices[] = $choice;
	}

	return $filtered_choices;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.