Is there a way to bold a specific word or part of a widget title? For example the title "Foo Bar", "Foo" here would be bold.

I tried doing the following, but the span gets outputted in the title:

add_filter( 'widget_title', 'test', 10, 1);
function test( $content ) {
    $test = explode(' ', $content);

    if ( !empty($test[1] ) ) {
        $title = '<span class="this_is_bold">' . $test[0] . '</span>';
        $title = $title . ' ' . $test[1];

    return $title;

This bolds the first words (or entire word, if only one) of my widget titles:

add_filter( 'widget_title', function ( $title ) {
    return preg_replace("/^(.+?)( |$)/", "<strong>$0</strong>", $title, 1);
}, 10, 1);

Breaking down the regex pattern, we have:

  • ^ is the start of the line
  • (.+?) is anything between our next group (in this context, a word)
  • ( |$) means either the first space, or the end of the line
    • the first space would be a normal title with multiple words
    • the end of line would mean a space wasn't found, so the title is just one word

In preg_replace(), we have a fourth attribute set to 1, it's the limit. This means the replace will only work on the first instance (which doesn't matter much in this context, as we know $title is always a single line so because of our start of line anchor ^ we know it can only work once always - but hey, why not).

Please keep in mind I put the code in an anonymous function for minimal code (showing off). If this is for a plugin or a public theme, best to stick to doing the function call as you originally did.

  • Thank you. I was testing it out and I am seeing the first word bold when I var_dump it, but the <strong> tag shows up within the actual title when the page renders.
    – cplus
    Jan 4 '18 at 18:07
  • What theme are you using? What plugins you got running? Works fine on Twenty-Seventeen. Jan 4 '18 at 18:28
  • Hmm, I've got a custom made theme running. What could be messing with it?
    – cplus
    Jan 4 '18 at 19:34

Normally WP won't allow html tags <> input but will allow "[" & "]". So a simple solution is to convert say "This is [b]bold[/b]" to "This is bold".

In your own widget you don't even need to use add_filter; something like:

$title = str_replace( array("[b]","[/b]"), array("<b>","</b>"), apply_filters('widget_title', $title));

(then, as usual, later rendering your widget title by echoing $title) should do the trick.

Your question refers to add_filter; if you need to use this function then this is simple and can easily be extended for other HTML tags:

function bold_widget_title( $title) { 
  $title = str_ireplace( array('[b]','[/b]') , array('<b>','</b>'), $title); 
  return $title; 
add_filter( 'widget_title', 'bold_widget_title' ); 
//  the arrays can be extended for other tags e.g "<br>" to allow you to choose where a long title is split to a new line.

It is possible to allow users to enter actual HTML tags but with hindsight I don't recommend it. In one widget I allow users to enter both <br> tags and [br] but how you do this is very specific to the widget.

Some pointers on how to do:

  1. tell WP to allow <b> tags in user title input e.g.

    $instance['title'] = wp_kses($new_instance['title'], array('b'=>array() ), array() );
  2. to avoid removal by filters: replace <b>s and </b>s with [] versions before saving

  3. make ready for rendering e.g.

    $title =  str_replace ( array("[b]","[/b]") , array("<b>","</b>") , apply_filters('widget_title', str_ireplace(array("[b]","[/b]"), array("<b>","</b>"),$title)));

Having seen your comments I now assume you want to alter any widget title irrespective of user input. So new answer (I've left the old which may be of use to others).

I am seeing the first word bold when I var_dump it, but the <strong> tag shows up within the actual title when the page renders

Like David I find code in functions.php correctly adds (non escaped) bold tags when add_filter is given a priority of 10 (as in your example).

On your site it appears that the function is adding your tags, but a later process (WP or some plugin) then escapes them converting < to &lt; etc.

Making your function run after other add_filters by "lowering" its priority might work e.g. add_filter('widget_title', 'test', 999999, 1);

If not, you could try this in your functions.php

remove_filter( 'widget_title', 'esc_html' ); // remove filter that may be added elsewhere

function bold_first_word( $title ) {  
  $title = trim(esc_html($title)); // re-apply escaping BEFORE bolding
  // or use wp_kses to remove tags except those specified (but don't allow bold tags)

  if (empty($title)) return $title;
  // some code to bolden first or only word (see below)
add_filter( 'widget_title', 'bold_first_word', 10, 1);

Code to bold first word without resorting to regular expressions:

$first_space = strpos($title, ' ');
if ($first_space !== false) {
    return '<strong>' . substr_replace($title, '</strong> ', $first_space, 1);
} else {
    return '<strong>' . $title . '</strong>';

Note: even if your function is working - it will not be called (i.e. work) with the minority of widget plugins that either do not apply_filter('widget_title', ... or that have some unusual title logic.

  • Yeah, I would like to alter the title without users having to do anything. I tried messing around with the code above and even changing the priority to see if anything changes, but it still shows up as <strong>Widget title here</strong>.
    – cplus
    Jan 8 '18 at 16:17
  • Ah, it works with a regular text widget, but not when the title is a link. Is it possible to do it when it's in an anchor tag? When I inspect it I see the following: <a href="[url here]"><strong>Foo </strong Bar</a>
    – cplus
    Jan 8 '18 at 16:42
  • Comment unclear. If you are saying the text strong appears (and is inserted by your new function) then it looks like the anchor and encoding of bold tags happens after. remove_filter( 'widget_title', 'esc_html' ); as per answer may help. If not it may be an unfriendly widget see last para of answer. What widget plugin is this? or are you talking about someone including link in text widget?
    – scytale
    Jan 10 '18 at 14:25
  • Mainly build-in plugins like the RSS Widget for example. It escapes the title even when using the remove_filter for it.
    – cplus
    Jan 10 '18 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.