WPML
SignaturePHP
$converted_url = apply_filters( 'wpml_permalink', string $permalink, string $language_code = null, bool $full_resolution = false );

Description

Filters a WordPress permalink and converts it to a language-specific permalink based on the language URL format set in the WPML language settings.

This means that when “Language URL format” is set to “Different languages in directories” the permalink will be returned in the http://domain.com/de form. When “A different domain per language” is selected the permalink will be converted to include the correct domain assigned to the requested language.

Arguments

  • $permalinkstringRequired

    The WordPress generated URL to filter.
  • $language_codestringOptional

    The language to convert the url into. It accepts a 2-letter code. When set to null, it falls back to default language for root page, or current language in all other cases. Default is null
  • $full_resolutionbooleanOptional

    Enable full conversion of hard-coded URLs.

Example Usage

Example 1

For example, if we have “Different languages in directories” selected, we can use the following code:

PHP
// will return http://domain.com/de/contact
$wpml_permalink = apply_filters( 'wpml_permalink', 'http://domain.com/contact', 'de' );

Example 2

We have a page http://example.org/hello/ and its translation http://example.org/fr/bonjour/. Let’s say we are calling the filter while we are displaying the home page http://example.org.

PHP
// displays http://example.org/fr/hello/ (wrong)
echo apply_filters( 'wpml_permalink', 'http://example.org/hello/', 'fr' ); 

// displays http://example.org/fr/bonjour/ (correct)
echo apply_filters( 'wpml_permalink', 'http://example.org/hello/', 'fr', true );

Important notes about the second example:

  • The full resolution is a heavy process, so WPML has to persist the resolved URLs in the database. The first call to wpml_permalink should significantly increase the database queries and load time. But the subsequent calls should not overload the process.
  • The database persistence is partially cleared when post/terms are updated or deleted. And it’s fully cleared when visiting SettingsPermalinks (this is a good troubleshooting option in case you encounter any issues).

How Full Resolution Converts Each URL

In WPML 5.4 and newer, full resolution (third argument true) works like this:

  • A post or term URL becomes the URL of its translation. This works in every request, REST requests included.
  • A post or term with no translation keeps its URL.
  • WPML converts a URL with no post or term behind it, such as the home URL or a date or author archive, as it does without the third argument.
  • A sticky link (?p=, ?page_id= or ?cat_ID=) to a deleted post or category returns unchanged.

For example, with “Different languages in directories” selected:

PHP
// displays http://example.org/fr/
echo apply_filters( 'wpml_permalink', 'http://example.org/', 'fr', true );