Last updated on October 18, 2023

Add a Custom Font Icon Set to the Theme

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Enhance design with font icons.

To add a custom font icon set to your WordPress theme, you can follow these steps:

  1. First, you need to obtain the font icon set files. This typically includes the font files (.woff, .woff2, .ttf, etc.) and the CSS file that defines the icons and their classes.

  2. Once you have the font icon set files, create a new directory in your theme’s directory to store these files. For example, you can create a directory named fonts or icons inside your theme’s directory.

  3. Copy the font files and the CSS file into the newly created directory.

  4. Open your theme’s functions.php file and add the following code snippet:

function wpsnippets_enqueue_custom_font_icon_set() {
    wp_enqueue_style( 'custom-font-icon-set', get_template_directory_uri() . '/fonts/custom-icon-set.css' );
}
add_action( 'wp_enqueue_scripts', 'wpsnippets_enqueue_custom_font_icon_set' );

Make sure to replace 'custom-icon-set.css' with the actual filename of your CSS file.

  1. Save the functions.php file and upload it to your theme’s directory.

Now, the custom font icon set is added to your theme. You can use the icons by adding the appropriate CSS classes to your HTML elements.

This code snippet registers and enqueues the custom font icon set CSS file in your theme. The wp_enqueue_style() function is used to enqueue the CSS file, and the get_template_directory_uri() function retrieves the URL of your theme’s directory. The add_action() function hooks the enqueueing of the CSS file to the wp_enqueue_scripts action, ensuring it is loaded on the front-end of your site.

Examples

Example 1: Adding a Custom Font Icon Set to the Theme

This use case demonstrates how to add a custom font icon set to a WordPress theme. The code example below registers the custom font icon set and enqueues the necessary stylesheets.

function wpsnippets_add_custom_font_icon_set() {
    wp_enqueue_style( 'custom-font-icons', get_template_directory_uri() . '/assets/css/custom-font-icons.css', array(), '1.0', 'all' );
}

add_action( 'wp_enqueue_scripts', 'wpsnippets_add_custom_font_icon_set' );

This code registers a custom font icon set by enqueuing the corresponding CSS file. The wp_enqueue_style() function is used to add the stylesheet to the theme. The first parameter is the handle for the stylesheet, the second parameter is the path to the CSS file, and the remaining parameters define the version, dependencies, and media type.

Example 2: Adding Multiple Custom Font Icon Sets to the Theme

This use case demonstrates how to add multiple custom font icon sets to a WordPress theme. The code example below registers two custom font icon sets and enqueues their respective stylesheets.

function wpsnippets_add_custom_font_icon_sets() {
    wp_enqueue_style( 'custom-font-icons-1', get_template_directory_uri() . '/assets/css/custom-font-icons-1.css', array(), '1.0', 'all' );
    wp_enqueue_style( 'custom-font-icons-2', get_template_directory_uri() . '/assets/css/custom-font-icons-2.css', array(), '1.0', 'all' );
}

add_action( 'wp_enqueue_scripts', 'wpsnippets_add_custom_font_icon_sets' );

This code registers multiple custom font icon sets by enqueuing their respective CSS files. The wp_enqueue_style() function is used to add each stylesheet to the theme. Each custom font icon set is given a unique handle, and their respective CSS files are enqueued with the appropriate handle.

Example 3: Adding a Custom Font Icon Set with Dependencies

This use case demonstrates how to add a custom font icon set to a WordPress theme with dependencies. The code example below registers the custom font icon set and enqueues its stylesheet, which depends on another stylesheet.

function wpsnippets_add_custom_font_icon_set_with_dependencies() {
    wp_enqueue_style( 'dependency-stylesheet', get_template_directory_uri() . '/assets/css/dependency-stylesheet.css', array(), '1.0', 'all' );
    wp_enqueue_style( 'custom-font-icons', get_template_directory_uri() . '/assets/css/custom-font-icons.css', array( 'dependency-stylesheet' ), '1.0', 'all' );
}

add_action( 'wp_enqueue_scripts', 'wpsnippets_add_custom_font_icon_set_with_dependencies' );

This code registers a custom font icon set by enqueuing its stylesheet, which has a dependency on another stylesheet. The wp_enqueue_style() function is used to add both stylesheets to the theme. The second parameter of the wp_enqueue_style() function specifies the dependencies as an array, with the handle of the dependent stylesheet included.

Last updated on October 18, 2023. Originally posted on December 9, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *