By default WordPress does not accept hyperlinks in the widget title. Even if you enter a hyperlink , 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 Snippet
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]', '</a>', $mytitle );
$mytitle = str_replace( ']', '>', $mytitle );
return $mytitle;
}
So now basically we can enter hyperlinks as [link href = http://google.com]My Widget Title[/link] in our widget title. Take note that we have entered the url without quotes.
This little snippet will make the widget title clickable. If you are not comfortable with coding then you can also achieve the same result by using plugins like Widget Title Links
- How to create 301 Redirects in WordPress - December 21, 2019
- How to Filter Blog Posts by Author in WordPress Admin panel - October 21, 2019
- How to convert / change post type in wordpress - October 21, 2019
Straight-forward, clean and no plugin. Very nice.
Thanks!
thanks
Hi, Is there a way to apply CCS to the clickable widget title created by using the snippet above? Specifically, I need to change the text color. Thanks. Amy B.
ANKIT . THANK YOU FOR POSTING ….
Your work is great, but…
How can I add the title atribute?
Thank you very much!
Thank you so much! I used to use a plugin for this, but it’s no longer maintained, so I’m thrilled to know a better way to do it.
Thanks buddy. It was great to add links but I also wanted to customize the Widgets title. Like I want to add span tag for separator do you know how to do that?
hell0 -the link worked, but it stripped all the visual customization of the title out. Any advice?
This greatly helped me out, thank you for posting this code snippet!
Great !! Works perfect in 2021! Thanks
Please is there anyway to add style on it like [link style=”color:red;float:right;” href = http://google.com]My Widget Title[/link] ?
Thanks. Waiting for your reply