Community Forums › Forums › Internationalization and Translations › Enqueue font from different sources dependant upon user IP.
Tagged: enqueue fonts, ip
- This topic has 2 replies, 2 voices, and was last updated 8 years ago by JeffCl.
-
AuthorPosts
-
August 26, 2016 at 2:59 am #192064JeffClMember
Hi there, I figured this belonged in Internationalisations, though not really Translations. If I'm wrong I'll repost in design tricks and delete from here.
I am in China. One of my sites is for Business Consultancy, my clients may be viewing the site from within China (behing the GFW) or from the rest of the free internet.
I am enqueueing Google Fonts, but this can cause some slowdown when a client is from China. Sometimes the font loads, sometimes it hangs then loads, sometimes it hangs doesn't load and falls back to standard fonts.
This isn't ideal.
There is a Chinese host that has a mirror of googles fonts that I can use in China, but then the situation is reversed, my Outside-Of-China visitors will be taxed trying to load their fonts from a Chinese server.
What I want to do is enqueue the fonts from a Chinese server if the user's ip is Chinese, and Google fonts if it's from elsewhere.
I have found this that detects a users IP address:
http://userinfo.io/As you will see I am not very adept with php, relying mostly on following other peoples examples and pulling bits together.
function fontsbyIP() { $in_china = false; $userinfo_json = file_get_contents('http://api.userinfo.io/userinfos') if ($userinfo_json) { $userinfo = json_decode($userinfo_json); if ($userinfo && is_object($userinfo) && $userinfo->country && $userinfo->country->code) { if ($userinfo->country->code == 'CN') { $in_china = true; } } } if ($in_china) { add_action( 'wp_enqueue_scripts', 'load_fonts_by_chinese_server' ); function 'load_fonts_by_chinese_server' () { wp_enqueue_style( 'qihoo-fonts', '//fonts.useso.com/css?family=Oxygen|Josefin+Sans', array(), CHILD_THEME_VERSION ); } } else { add_action( 'wp_enqueue_scripts', 'load_fonts_by_non_chinese_server' ); function 'load_fonts_by_non_chinese_server' () { wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family=Oxygen|Josefin+Sans', array(), CHILD_THEME_VERSION ); } } }
I have tried this in a variety of different formats, but always getting an http 500 error.
At the moment, this site is doing okay as it's using cloudflare, which has an agreement in China and I believe that is helping out here.
However, I would love to be able to get this working as an alternative. It's on my list to learn, once I've gotten a few necessary projects out of the way. Having said that, I'd be ecstatic if anyone could give me some pointers here.
Best
Jeff
http://ezuconsulting.comAugust 26, 2016 at 7:09 am #192072Victor FontModeratorWhy not just load the fonts locally instead of from the internet? This explains how: http://victorfont.com/use-google-fonts-locally/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?August 26, 2016 at 8:48 am #192081JeffClMemberHi Victor,
Thanks for your advice, I may well do that.
But given my hosting is in the USA, and it also has issues with the GFW at times, it would be good to be able to enqueue from China for Chinese users.
Jeff
-
AuthorPosts
- You must be logged in to reply to this topic.