Adding Custom Fonts to WordPress or any Website.

A majority developers that are asked to try custom fonts for WOFF and TTF for testing purposes might say “Nope, Nope, Nope, Nah, No, never, nay, NO!” I’m using Google fonts and stick it in the header.

I know what you mean. That’s exactly what I would do if it’s not worth it.

Why even bother using some proprietary font. So that’s when you get a client that needs an exact font they purchased on Adobe type kit or downloaded from some crazy font website. We’re going go with that route of crazy weird font that your client wants.

So I can’t think of some crazy font right now, and plus cause I don’t really care that much but let’s use the framework that font squirrel uses.

We start by getting all the necessary fonts like .eot, .woff, .ttf, .svg, .woff2, and maybe even otf.  So here’s an example if you know the basics of how fonts works and if you don’t- check out w3 or w3 schools if you want to learn more. You could be lazy and just get one ttf file but then again, there’s browsers that need more info than so it’s not worth. You can always look for a converter as well.

So for those that already know how to use the font rule, let’s begin by looking at the code.



@font-face {
    font-family: 'Crazy wierd';
    src: url('fonts/Crazy-wierd-webfont.eot');
    src: url('fonts/Crazy-wierd-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/Crazy-wierd-webfont.woff2') format('woff2'),
         url('fonts/Crazy-wierd-webfont.woff') format('woff'),
         url('fonts/Crazy-wierd-webfont.ttf') format('truetype'),
         url('fonts/Crazy-wierd-webfont.svg#Crazy-wierd') format('svg');

}

h1,h2,h3,h4,h5,h6
{
font-family: 'Crazy wierd', sans-serif;
text-transform: capitalize;
}

Pretty basic, so now that you have a basic setup if you are putting it a basic website. Create a fonts folder and just do your HTML5/CSS like you always do it.

So now what about WordPress?  There some advanced ways to do it and the basic way. I’ll go in the advanced way and then back to the the basic.

The first thing you run into is the Functions.php file where you can have the default theme add styles and fonts.



function ninja_scripts() {
wp_enqueue_style( 'ninja-fonts', get_template_directory_uri().'/inc/css/ninja-style.css' );
}

add_action( 'wp_enqueue_scripts', 'ninja_scripts' );

//so now you can create a folder called 'fonts' and add your fonts like eot/ttf/etc
//into -> wp-content -> themes -> yourtheme -> inc - > css - > fonts

So that’s alright but what happens if you update your WordPress theme and the functions.php file gets replaced with a new one, then you are totally SOL.

Let’s think of way that keeps all the codes in a child theme.

Why would you use a child theme? well that’s simple- keep all your stuff organized.
The easiest way is to put in a child theme. If you don’t know how? here’s a very good tutorial on wordpress.org

I made this post just so you can copy and past the quick code and paste it into your child theme so you don’t have to do a bunch of research. You could hard code everything but that’s not recommended with themes. It’s that update that will wipe out your work and it will happen, not if but when.