• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Enqueue font from different sources dependant upon user IP.

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Internationalization and Translations › Enqueue font from different sources dependant upon user IP.

This topic is: not resolved

Tagged: enqueue fonts, ip

  • This topic has 2 replies, 2 voices, and was last updated 9 years, 5 months ago by JeffCl.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • August 26, 2016 at 2:59 am #192064
    JeffCl
    Member

    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.com
    August 26, 2016 at 7:09 am #192072
    Victor Font
    Moderator

    Why 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 #192081
    JeffCl
    Member

    Hi 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

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble