Extra Catchy

How to Link Widget Titles in WordPress without using a Plugin

Do you want to give the link to your WordPress widget title whatever link you want to give? Then you are at right place.

By default, WordPress does not accept hyperlinks in the widget title. Even if we try to enter a hyperlink in the place where we give the name to widget it will strip down all the HTML tags and simply print out the text.  So basically in order to link the widget titles, we’ll need to find a way to make WordPress accept HTML in Widget Title.

The Code Snippet of Add Link to Widget Title

Here is a little snippet which lets you add a hyperlink in the widget title. The snippet makes use of widget_title filter and it will dynamically replace custom tags with actual HTML tags. You can use the Code Snippet plugin to run this piece of code.

// We will make use of widget_title filter to 
//dynamically replace custom tags with html tags

add_filter( 'widget_title', 'accept_html_widget_title' );

function accept_html_widget_title( $mytitle ) { 

  // The sequence of String Replacement is important!!
  
	$mytitle = str_replace( '[link', '<a', $mytitle );
	$mytitle = str_replace( '[/link]', '', $mytitle );
    $mytitle = str_replace( ']', '>', $mytitle );
	

	return $mytitle;
}

With the above code snippets now basically we can enter hyperlinks as in[link href = http://google.com]My Widget Title[/link] our widget title. Notice that we have entered the URL without quotes.

This little snippet will make the widget title clickable and link-able. If some of you guys are not comfortable with coding then you can also achieve the same result by using plugins like Widget Title Links

Parth Parikh

function accept_html_widget_title( $mytitle ) {

// The sequence of String Replacement is important!!

$mytitle = str_replace( ‘[link’, ‘<a', $mytitle );
$mytitle = str_replace( '[/link', '‘, $mytitle );

return $mytitle;
}

Parth Parikh

function accept_html_widget_title( $mytitle ) {

// The sequence of String Replacement is important!!

$mytitle = str_replace( ‘[link’, ‘<a', $mytitle );
$mytitle = str_replace( '[/link', '‘, $mytitle );

return $mytitle;
}

Correct code

Moby

Hey there … thanks for this, but sadly I can’t get it to work. When I put in the code in square brackets it all just displays like that on the live site, but isn’t a link. I’m a bit stumped!