Support Log in

5. URL Functions

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

Functions for constructing language-specific URLs and understanding the URL format configuration.

LF_Core::get_language_url($language)

Returns the URL for the current page in the specified language.

php
/**
 * Get the URL for a specific language on the current page.
 *
 * On singular pages, resolves to the translated post's permalink.
 * On archives, builds the URL from the current path with the
 * language prefix/param adjusted.
 *
 * @param  string $language Language code
 * @return string Full URL for the requested language
 */
$core = LF_Core::instance();
$en_url = $core->get_language_url('en');
$ru_url = $core->get_language_url('ru');
ParameterTypeDescription
$languagestringTarget language code
Return TypeDescription
stringFully qualified URL for the given language on the current page
Example 1: Custom hreflang tags
php
// Lang Forge already outputs hreflang tags automatically.
// This example is for custom implementations or overrides:
add_action('wp_head', function () {
    $languages = lf_get_languages();
    $core      = LF_Core::instance();

    foreach ($languages as $code => $lang) {
        $url = esc_url($core->get_language_url($code));
        $hreflang = str_replace('_', '-', $lang['locale']);
        echo '<link rel="alternate" hreflang="' . esc_attr($hreflang) . '" href="' . $url . '">' . "n";
    }

    // Add x-default pointing to the default language
    $default = lf_get_default_language();
    $default_url = esc_url($core->get_language_url($default));
    echo '<link rel="alternate" hreflang="x-default" href="' . $default_url . '">' . "n";
});
Example 2: Build a language-aware URL helper for templates
php
// Build a language-aware URL for a custom page
function theme_get_localized_url($path) {
    $lang = lf_get_current_language();
    $default = lf_get_default_language();
    $format = get_option('lf_url_format', 'directory');

    if ($lang === $default) {
        return home_url($path);
    }

    switch ($format) {
        case 'directory':
            return home_url('/' . $lang . $path);
        case 'parameter':
            return add_query_arg('lang', $lang, home_url($path));
        default:
            return home_url($path);
    }
}

// Usage
echo '<a href="' . esc_url(theme_get_localized_url('/contact/')) . '">Contact</a>';

URL Format Modes

Lang Forge supports four URL format modes, configured in Settings > URL Format:

Directory Format (default):
text
https://example.com/          (default language - clean URL)
https://example.com/en/       (English)
https://example.com/en/about  (English - About page)
https://example.com/de/blog   (German - Blog page)
Parameter Format:
text
https://example.com/                (default language)
https://example.com/?lang=en        (English)
https://example.com/about?lang=en   (English - About page)
Subdomain Format:
text
https://example.com/        (default language)
https://en.example.com/     (English)
https://de.example.com/     (German)
Domain Mapping:
text
https://example.com/       (default language)
https://example.co.uk/     (English)
https://example.de/         (German)

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