Define page language attribute based on page ID or URL
Setting the WordPress page language attribute for a specific page based on the ID or URL
Note: The below filter hook is only useful whenever you are not using a translation plugin
When you are not using any language plugin to translate your website but still want to have a specific page in a different language (other than your main site language), you will have to change the <html>
language attribute based on the current page URL. This is important so that whenever a user visits a page that is different from your main site language the browser won't try to translate the page (again) including your form. Let's say your site is in English by default, but you also have a translated version of your form in Deutsch and French. When the user visists the page /de/contact
or /fr/contact
the browser will still think that the page is in your main language (English) because the language attribute would still be set to en
. This can cause unexpected translations being done by the browser. The only way to avoid this is to make sure the correct language attribute for these pages is set accordingly. A filter hook on how to do this can be found below.
Last updated