
Use Widget Title as a Hyperlink in WordPress
- October 15, 2015
- Leave a comment
Widgets are counted as one of the main features of WordPress and plays an important role in WordPress Development. A widget is defined as a little section of a website which performs a specific task. We can add these widgets in different sidebars of a web page. WordPress has some built in widgets which are recent posts, calendar, tag cloud, search, navigation menu, etc.
Most of the displayed widgets have a common title field in which a user can write any text title or caption according to his/her need. In this title field, WordPress does not allow a user to use a Hyperlink in it by default. It will automatically remove all the HTML tags and only allows a user to put the text content in it.
Lets assume a situation that you have a widget in which the title should be a Hyperlink. Now in order to add a Hyperlink in the title field, you need to write a little bit of code. You can add the following chunk of code into your functions.php file (also you can use this code in any child theme):
1 2 3 4 5 6 7 8 9 10 |
/** This function will allow us to write Hyperlink in Widget Title **/ /** This function will allow us to write Hyperlink in Widget Title **/ add_filter( 'widget_title', 'anchor_in_widget_title' ); function anchor_in_widget_title($widgetTitle) { // We are replacing with [anchor][/anchor] $widgetTitle = str_replace( '[anchor', '<a', $widgetTitle ); $widgetTitle = str_replace( '[/anchor]', '', $widgetTitle ); $widgetTitle = str_replace( ']', '>', $widgetTitle ); return $widgetTitle; } |
Once you are done, go to your Widgets page from WordPress admin panel (Appearance -> Widgets). Add any widget which has a title field. After this, add the following line of code in your title. Make sure that the URL should be without quotes. The following screenshot will explain better:

This small chunk of code will make your widget title clickable. If this code does not suits your requirement then you can also use “Widget Title Links” plugin for this task.
What changes are needed to make the link open in a new tab?
Thanks
Hello Susan,
Thanks for contacting us.
Just add target=_blank in your [anchor] tag after the href attribute.
Regards,
Abdullah | WP Plugin Developer
Email: support@presstigers.com
HI,
The plugin does not work with latest wordpress. I gives an option add url but does not work.
Also tried to add code in to function.php Does not help.
Any idea?
Hi Mihir,
Thanks for pointing out the issue.
We have revised the code, there were some extra spaces that caused the error. Please have a look at it.
Let us know if the issue still persists.
Regards,
—
Ahmed Sarfraz | Associate Plugin Developer
Email: support@presstigers.com
Thanks a lot, worked perfectly.
It does make the title into a link, but also all the text in the box below the the widget title.
Thanks for this… It is working for me… _/\_