How can I add links to widget titles?

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.

Read More

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

Related posts

Leave a Reply

5 comments

  1. 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!

  2. 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.

  3. 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>