So, here is a little snippet of code that would allow you to add any HTML tag to your WordPress post titles as you see fit.
Useful for when you need to have a bolded word or line break at a specific place in the text.
Step 1: create your shortcode. Here are a few handy ones we use on WordPressHeroes.io
/**
* Function to allow for HTML in widget titles
* using a shortcode you can design yourself
*
* usage:
*
* something
* something
* something
* [anchor url="https://domain.com/"]something[/anchor]
*
* Most of the widgets use a filter on widget title widget_title, but some custom widget might not apply or use this hook.
* On those widgets, this method will not work.
*
* source: https://wordpress.stackexchange.com/a/136800/2015
*/
// Original snippet
add_filter('widget_title', 'do_shortcode');
add_filter('the_title', 'do_shortcode');
add_shortcode('br', 'so_shortcode_br');
function so_shortcode_br( $attr ){ return '
'; }
add_shortcode('span', 'so_shortcode_span');
function so_shortcode_span( $atts, $content ){
$span = shortcode_atts( array(
'color' => '',
), $atts );
return ''. $content . '';
}
add_shortcode('strong', 'so_shortcode_strong');
function so_shortcode_strong( $atts, $content ){
$a = shortcode_atts( array(
'class' => 'someclass',
), $atts );
return ''. $content . '';
}
add_shortcode('b', 'so_shortcode_b');
function so_shortcode_b( $atts, $content ){
$b = shortcode_atts( array(
'color' => '',
), $atts );
return ''. $content . '';
}
Step 2: filter the HTML tag back out so that it doesn’t show up in your url’s ortags or any other place for that matter
//wp_title clean-up
function render_wp_title($title){
echo preg_replace("~(?:[/?)[^/]]+/?]~s", ' ', $title);
}
add_filter( 'wp_title', 'render_wp_title', 98 );
Step 3: Add your titles … something like, “Uuuu, look at me, I’m a [ b ] cool [ /b ] title”