How to install and use Ionicons in WordPress

Ionicons is a web component, so a stylesheet alone shows nothing. Load Ionicons 8 in WordPress with wp_enqueue_script_module, then add icons with one tag.

On this page
  1. Load the script as a module
  2. Where WordPress prints it
  3. Use the icons
  4. Why the icon vanishes for some users
  5. Check what your theme already loads

The old version of this post told you to enqueue an Ionicons stylesheet from unpkg. I opened that URL this week and it returns a 404. Even when it worked, it couldn’t draw a single icon.

Current Ionicons isn’t a font. An icon is an <ion-icon> element, and that element is a web component. The browser only knows what to do with it after the Ionicons script runs. No script, no icon, whatever CSS you load.

So I rebuilt the setup on a test site with WordPress 7.1.4 and Ionicons 8.1.0, the current release. It’s one function and one HTML tag.

Load the script as a module

The Ionicons usage page loads one file, ionicons.esm.js, with type="module". WordPress has had a function for exactly that since 6.5: wp_enqueue_script_module(). Add this to your child theme’s functions.php or a code snippets plugin like WPCode:

PHP
/**
 * Load the Ionicons web component on the front end.
 */
function shameem_enqueue_ionicons() {
	wp_enqueue_script_module(
		'ionicons',
		'https://unpkg.com/ionicons@8.1.0/dist/ionicons/ionicons.esm.js',
		array(),
		null
	);
}
add_action( 'wp_enqueue_scripts', 'shameem_enqueue_ionicons' );

The version number lives in the URL, so the fourth argument is null. With null, WordPress adds no ?ver= to the URL. Leave it out and WordPress adds its own version number instead.

Keep the exact version in the URL. A URL without one follows whatever Ionicons ships next, and a URL pinned to ionicons@8.1.0 keeps your pages the same until you choose to update.

Older guides also add a second nomodule script tag that points at ionicons.js. That file doesn’t exist in 8.1.0. Every current browser runs modules, so the one tag is enough.

Where WordPress prints it

WordPress decides where the module tag goes, in WP_Script_Modules::add_hooks() (wp-includes/class-wp-script-modules.php). Block themes get it in the <head>. Classic themes get it in the footer.

On my test site with Twenty Twenty-Five, the page printed this in the head:

HTML
<script id="ionicons-js-module" src="https://unpkg.com/ionicons@8.1.0/dist/ionicons/ionicons.esm.js" type="module"></script>

Module scripts don’t block the page from rendering, so either spot is fine. The script then fetches each icon’s SVG file from the same unpkg folder, and only for icons that are on screen.

That does mean every visitor’s browser talks to unpkg. If you’d rather not send your visitors to a third-party CDN, host the files yourself and change the URL.

Use the icons

Pick an icon on ionic.io/ionicons and copy its name. Most icons come in three styles: heart, heart-outline and heart-sharp.

HTML
<ion-icon name="call-outline" aria-hidden="true"></ion-icon>

Add aria-hidden="true" when the icon sits next to text that already says the same thing. Size and color come from plain CSS:

CSS
ion-icon {
	font-size: 32px;
	color: #2271b1;
}

In a theme template, write the tag straight into the PHP file. In a post or page, use a Custom HTML block.

Why the icon vanishes for some users

I ran the same markup through wp_kses_post(), the filter WordPress uses on post content from users without the unfiltered_html capability. It came back as <p>Call us </p>. The <ion-icon> tag was gone, because it isn’t on the list of HTML tags WordPress allows.

On a single site, administrators and editors have unfiltered_html, so their icons stay. Authors and contributors don’t, so theirs get stripped when they save. On multisite, only super admins have it. If DISALLOW_UNFILTERED_HTML is set in wp-config.php, nobody has it.

For sites where authors write the posts, put icons in templates, patterns or a shortcode instead of post content.

Check what your theme already loads

Before adding Ionicons, search your theme for dashicons, font-awesome and ionicons. Two icon libraries on one page means two sets of files for the same job, and every extra stylesheet in the <head> is render-blocking CSS that slows LCP.

If you only need a handful of icons, there’s an even lighter route. Every Ionicons icon is also a plain SVG file in the package, under dist/svg/. Paste the SVG into a template file and you need no script at all.

Did this answer your question?

If not, tell me what you were looking for. Your question might be my next post.

What were you looking for?

Shameem Reza

Shameem Reza Happiness Engineer at Automattic

I fix WooCommerce problems for a living and review plugins for WordPress.org. Every fix here is tested on a clean site first.

No comments yet. Ask the first question.

No account needed. Comments appear after a quick check.