X

How To Add Simple HTML to Widget Titles

Snippets by IsItWP

Are you looking for a way to add simple HTML to widget titles? Normally you can’t add HTML in widget titles, as the HTML code is removed. This snippet lets you add strong and em tags to your widget titles.

Instructions:

  1. Add this code to your theme’s functions.php file or in a site-specific plugin.
  2. User the codes [s]text[/s] and [e]text[/e] on your text. [s]text[/s] will become <strong>text</strong> and [e]text[/e] will become <em>text</em> in the HTML. If you are familiar with coding, you can also add more tags to the snippet, but be sure to keep security in mind.
<?php
function html_widget_title( $title ) {
	//HTML tag opening/closing brackets
	$title = str_replace( '[', '<', $title );
	$title = str_replace( '[/', '</', $title );

	//<strong></strong>
	$title = str_replace( 's]', 'strong>', $title );
	//<em></em>
	$title = str_replace( 'e]', 'em>', $title );

	return $title;
}
add_filter( 'widget_title', 'html_widget_title' );
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: WPForms vs. Gravity Forms – Best Form Plugin Showdown.

Comments   Leave a Reply

  1. Thank you very much, could you please tell me what to add for making a line break in widget title with ?
    Thank you!

    1. If I understand the question, you could possibly use this line:

      $title = str_replace( ‘?’, ‘
      ‘, $title );

  2. Prasun Bhattacharya August 20, 2019 at 9:04 am

    Thank you very much….:)

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!