5

I'm using three widgets, two of them are default WP widgets (Recent Posts and Archives) and the third one is a custom widget which acts like Recent Posts but for a specific custom Post Type.

My question is how can I make the widget titles clickable links? It seems that WordPress does not accept HTML in the title field and automatically removes it. Ideally I don't want to do this with a plugin even though I know the likes of Linkable Title HTML and PHP Widget exist. It isn't really what I'm after.

The code for the custom widget I have so far can be seen here: http://pastie.org/1847651

3
  • Ian, can you include the code for the widget you created?
    – EAMann
    Apr 29, 2011 at 15:44
  • I've just updated my question with a link to the widget code. Although I need to add links to the title of this custom widget I would also like to add links to the default 'Recent Posts' and 'Archive' widget too if possible.
    – Ian Young
    Apr 29, 2011 at 15:51
  • this is working fine thank you although I can't find my simple text widgets in the plugin folder. I am new to all this.. where can I located them? Also, does anyone know how to convert a category name (which doubles as a heading as part of a theme) as a link too? Thanks in advance. Jac
    – user13862
    Mar 6, 2012 at 6:12

5 Answers 5

3

Making the title click-able

WordPress won't let you pass HTML code in the title of the widget, but it does have the handy parameters $before_title and $after_title that you can use to manipulate things.

In your widget, just add the first part of the link (<a href="...">) to the end of $before_title and the last part of the link (</a>) to the beginning of $after_title and your title will automatically be linked.

You do this after you extract the arguments in the widget() method ... so in your code, find the section:

ob_start();
extract($args);

$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Staff News') : $instance['title'], $instance, $this->id_base);

And add:

$before_title .= '<a href="http://something.com">';
$after_title = '</a>' . $after_title;

Then you'll be linked!

Changing the default widgets

You can't apply this method to the default widgets directly, but there is a workaround using object inheritance. Just define your own custom widgets that extend the default widgets:

class My_Widget_Recent_Posts extends WP_Widget_Recent_Posts {

}

You'll have to redefine the widget name and such in the constructor, then redefine the widget() method by copying what's already there but applying our $before_title/$after_title hack from above. Your new version of the default widget will have linkable titles!

3
  • That's awesome, just what I was after! I've got my custom Post Types widget to work with the clickable link title but when it comes to extending the default Recent Posts widget I'm not quite there yet. I've extended the default class and registered the new widget but it doesn't show up in my widget panel. The code I've added to my functions.php file is: pastie.org/1848212
    – Ian Young
    Apr 29, 2011 at 18:47
  • Can you see where I am going wrong in extending the default widget class? I've extended the class, renamed the widget in the constructor, and copied over the contents from the default Recent Posts widget. No errors thrown but it doesn't appear in the Widget panel. I've also registered the new widget.
    – Ian Young
    Apr 30, 2011 at 9:57
  • You have $this->WP_Widget('recent-posts', __('My Recent News Posts'), $widget_ops); in the constructor. You should have $this->WP_Widget('widget_my_recent_entries', __('My Recent News Posts'), $widget_ops);.
    – EAMann
    Apr 30, 2011 at 16:54
1

If the default widgets don't offer the functionality you are after, specifically allowing custom links on titles, you might best be served by writing your own.

http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28

http://churchco.de/the-complete-guide-to-writing-a-wordpress-widget/

This usually takes the form of writing a plugin that adds custom widgets to the widget screen. Making a small adjustment to an existing widget is as simple as copying the code to the original and including it in your modified way in a plugin.

Default widgets are located in wp-includes/default-widgets.php

Edit If you aren't familiar with WordPress code, you need to decide if you want to become more familiar. If so, you need to add a field to the widget form that allows the user to enter the link address, and you need to edit the widget code to insert the link when the widget code outputs the title.

If you don't want to become familiar, then I believe, you're out of luck. Widgets don't come with the ability to modify their capabilities without modifying the WordPress code.

1
  • Well I've created one widget from this example but I'm not familiar enough with WP code to be able to know how to modify it to have a clickable title. Any suggestions?
    – Ian Young
    Apr 28, 2011 at 13:06
1

I just released a plugin that let's anybody add a link in the widget settings. It works with most plugins. Get it here: http://wordpress.org/extend/plugins/widget-title-links/

0

Actually, you CAN put html directly into the header. With a bit of coding. Check out this article that shows you how:

http://ponderwell.net/2011/05/how-to-use-html-in-wordpresss-widgets-title/

1
-1

or you can simply use a plain text widget that allows HTML in the contents and leave the title blank, then add at the top of that widgets content...

<a href="yourlink"><div class="widget-title">Your Title</div></a>
1
  • 1
    Not really the same. The OP wants to link the widget title, not anything in the widget content. Nov 29, 2012 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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