I’ve already speak in a previous post about the WordPress theme I’ve created to redirect visitors browsing ”http://oncleben31.cc” to the french or english section depending on the preferred language of the browser.
This theme is a quick and dirty hack for my bilingual blog and I think it can be helpful for other people like me who trying to make a multilingual WordPress website. So I’ve decided to share my code on Githhub and encourage you to test it and improve it. The code is commented and section you have to update are clearly identified.
Currently the theme works as below:
- IF the visitor requests yourwebsite.com/ THEN:
- IF the preferred browser language is French THEN visitor is redirected to yourwebsite.com/fr/.
- ELSE visitor is redirected to yourwebsite.com/en/.
- IF the visitor requests an URL like yourwebsite.com/fr/xxx or yourwebsite.com/en/yyy THEN websites of the WordPress network manage the request.
- IF the visitor requests yourwebsite.com/zzz where zzz doesn’t start with fr/ or en/ THEN:
- IF a static page exists it’s returned to the visitor.
- ELSE:
- IF the preferred browser language is French THEN the visitor is redirected to yourwebsite.com/fr/erreur404 which displays a 404 error message in french.
- ELSE the visitor is redirected to yourwebsite.com/en/404error which displays a 404 error message in english.
I’ve many ideas to improve the theme and make it more easy to use but I haven’t any time and competency to do that. If this theme is useful for you and you want to improve it, fork my Github repo and send me pull requests.
- Use the theme settings in the WordPress administration dashboard to set the languages and the redirection URL.
- It could be better to not use only the prefered language but all the language managed by the browser with a priority list.
- Manage an ignore list to allow the WordPress root website to generate some files (robots.txt or sitemap.xml for example)
I hope you will be many to use it and improve it.
Source on Github : https://github.com/oncleben31/Multisite-Language-Switcher-Theme
No related posts.
Français