Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Ian, can you include the code for the widget you created? –  EAMann Apr 29 '11 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 Apr 29 '11 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 '12 at 6:12
add comment

7 Answers

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!

share|improve this answer
    
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 Apr 29 '11 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 Apr 30 '11 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 '11 at 16:54
add comment

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.

share|improve this answer
    
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 Apr 28 '11 at 13:06
add comment

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/

share|improve this answer
add comment

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/

share|improve this answer
    
Welcome to WordPress Stack Exchange, Alfred! We have a helpful FAQ here: An answer should be more than barely more than a link to an external site. Could you add a basic explanation to your answer? –  toscho Mar 3 '12 at 9:05
add comment

I think that the solution here would have solved this easily. You can specify the URL and title separated with a | in the title, and the filter recognizes it and applies the link to the title.

Works for all widgets, out of the box, downloaded plugins and your own custom ones.

share|improve this answer
add comment

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>
share|improve this answer
1  
Not really the same. The OP wants to link the widget title, not anything in the widget content. –  chrisguitarguy Nov 29 '12 at 22:53
add comment
$instance['title'] = strip_tags($new_instance['title']);

remove this line.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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