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:
- Add this code to your theme’s functions.php file or in a site-specific plugin.
- 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.
Thank you very much, could you please tell me what to add for making a line break in widget title with ?
Thank you!
If I understand the question, you could possibly use this line:
$title = str_replace( ‘?’, ‘
‘, $title );
Thank you very much….:)