Support Log in

6. WPML Compatibility Functions

Developer Guide
Prefer video? Watch the Lang Forge tutorials 9 short guides covering every feature Watch

Lang Forge provides drop-in WPML compatibility functions so that themes and plugins built for WPML work without any code changes. These functions are only defined when WPML is not active, so there is no conflict if WPML is installed alongside Lang Forge (e.g., during a gradual migration).

The compatibility layer covers the most commonly used WPML API functions. If your theme or a third-party plugin calls any of these, it will work transparently with Lang Forge.

icl_get_languages($args)

Returns the same data structure that WPML themes expect, making it the most frequently used WPML compatibility function.

php
/**
 * WPML-compatible: Get all languages with URLs and flag data.
 *
 * @param  string|array $args Optional. 'skip_missing' => 0, 'orderby' => 'custom'
 * @return array Associative array keyed by language code. Each entry contains:
 *               id, code, active, native_name, translated_name,
 *               language_code, country_flag_url, url
 */
$languages = icl_get_languages('skip_missing=0');

foreach ($languages as $lang) {
    echo '<a href="' . esc_url($lang['url']) . '">';
    echo '<img src="' . esc_url($lang['country_flag_url']) . '" alt="" width="20" height="14"> ';
    echo esc_html($lang['native_name']);
    echo '</a>';
}
ParameterTypeDescription
$argsstringarrayOptional. Query string or array with skip_missing and orderby keys.
Return TypeDescription
arrayAssociative array keyed by language code with WPML-compatible fields
Return format:
php
[
    'en' => [
        'id'               => 'en',
        'code'             => 'en',
        'active'           => 1,    // 1 if current language, 0 otherwise
        'native_name'      => 'English',
        'translated_name'  => 'English',
        'language_code'    => 'en',
        'country_flag_url' => 'https://example.com/wp-content/plugins/langforge/assets/flags/gb.svg',
        'url'              => 'https://example.com/en/',
    ],
    // ...
]
Example 1: WPML-style language switcher from a theme
php
// This code from a WPML-compatible theme works unchanged:
$languages = icl_get_languages('skip_missing=0&orderby=custom');
if (!empty($languages)) {
    echo '<div class="language-bar">';
    foreach ($languages as $lang) {
        $class = $lang['active'] ? 'current-lang' : '';
        echo '<a href="' . esc_url($lang['url']) . '" class="' . $class . '">';
        echo '<img src="' . esc_url($lang['country_flag_url']) . '" alt="' . esc_attr($lang['native_name']) . '"> ';
        echo esc_html($lang['native_name']);
        echo '</a> ';
    }
    echo '</div>';
}
Example 2: Skip languages with no translation for the current page
php
$languages = icl_get_languages('skip_missing=1');
// Only languages that have a translation for the current page are returned.
// On archive pages, all languages are returned since archives are dynamically filtered.

icl_object_id($element_id, $element_type, $return_original, $language_code)

Returns the translated post/page ID for a given language. This is the most commonly used WPML function in theme code.

php
/**
 * WPML-compatible: Get translated post ID.
 *
 * @param  int         $element_id      Original post/page ID
 * @param  string      $element_type    'post', 'page', or any post type slug
 * @param  bool        $return_original Return original ID if no translation exists
 * @param  string|null $language_code   Target language (default: current language)
 * @return int|null    Translated post ID
 */

// Get the French version of post #42
$french_id = icl_object_id(42, 'post', false, 'fr');

// Get the translation in the current language, or the original if none exists
$translated_id = icl_object_id(42, 'page', true);
ParameterTypeDescription
$element_idintOriginal post/page ID
$element_typestringPost type slug: 'post', 'page', 'product', etc.
$return_originalboolIf true, returns the original ID when no translation exists. If false, returns null.
$language_codestringnullTarget language code. Defaults to the current language.
Return TypeDescription
intnullTranslated post ID, or null (or original ID if $return_original is true)
Example 1: Translate the front page ID for a menu
php
// This code from a WPML theme works unchanged with Lang Forge:
$page_id = icl_object_id(get_option('page_on_front'), 'page', true);
$home_url = get_permalink($page_id);
echo '<a href="' . esc_url($home_url) . '">Home</a>';
Example 2: Build a cross-language page map
php
// Get all translations of key pages
$pages = [
    'about'   => 10,
    'contact' => 15,
    'faq'     => 22,
];

$languages = lf_get_languages();
$page_map = [];

foreach ($pages as $slug => $id) {
    foreach ($languages as $code => $lang) {
        $translated_id = icl_object_id($id, 'page', false, $code);
        if ($translated_id) {
            $page_map[$slug][$code] = $translated_id;
        }
    }
}
// $page_map = ['about' => ['ru' => 10, 'en' => 45], 'contact' => ['ru' => 15, 'en' => 46], ...]

icl_get_current_language() / icl_get_default_language()

php
/**
 * WPML-compatible: Get current language code.
 * @return string Current language code
 */
$lang = icl_get_current_language();

/**
 * WPML-compatible: Get default language code.
 * @return string Default language code
 */
$default = icl_get_default_language();

icl_language_selector action

Themes that use WPML’s do_action('icl_language_selector') will automatically get Lang Forge’s switcher:

php
// In a theme template - this works with both WPML and Lang Forge
do_action('icl_language_selector');

> Note: The WPML compatibility layer maps directly to Lang Forge’s native functions. There is no performance penalty for using WPML functions.

Forge AI Assistant Online

Hi! I'm the Lang Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs