WPML Version: 3.2.2
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/deform. When “A different domain per language” is selected the permalink will be converted to include the correct domain assigned to the requested language.
- Retrieving Localized Content
- (string) (Required) The WordPress generated URL to filter.
- (string) (Optional) 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
- (boolean) (Optional) Enable full conversion of hard-coded URLs.
- hook example usage:
For example, if we have “Different languages in directories” selected, we can use the following code:
// will return http://domain.com/de/contact $wpml_permalink = apply_filters( 'wpml_permalink', 'http://domain.com/contact', 'de' );
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.
// 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 Settings -> Permalinks (this is a good troubleshooting option in case you encounter any issues).
- In case the URL could not be resolved, the filter wpml_permalink will return the original URL.