How can I make the title of widgets in my sidebar a link to another page?

Is there a way to make the title for my widgets link to another page. For example I have groups widget activated and would like for people to be able to click on the title to take them to the groups page. Same for the rest of my widgets. Thanks for any help that can be provided.

  • DavidM
    • DEV MAN’s Mascot

    Hi jsartwell,

    As far as I can see you’d have to edit the widget’s code. With many widgets, if a title isn’t provided, it doesn’t show anything in the title area, including surrounding graphics. As such, you might be able to get away with using a Text widget, including your link in the body of that widget, and not specifying a title for it. It could somewhat emulate another widget’s title area that way.

    I’ve used that trick myself, but it really depends on the theme you’re using as to whether it’ll work effectively or not.

    Cheers,

    David

  • jsartwell
    • WPMU DEV Initiate

    Thank you for your help. I was able to find the portion of the widget code that needed to be changed and it worked perfectly. Just in case others need help with this.

    I changed this:

    echo $before_widget;
    echo $before_title
    . $widget_name
    . $after_title; ?>

    to this:

    echo $before_widget;
    echo $before_title;
    echo '<a href="http://yoururl">TITLE</a>';
    echo $after_title; ?>

    Hope this helps someone else out. Thanks again for your quick response.

  • maxaud
    • The Crimson Coder

    jsartwell, here is a plugin you can activate that will allow you to modify widget titles adding links to each of them. When editing the title in WordPress Appearance->Widgets panel enter the title like so to create a link for it

    http://www.domain.com|The title of the widget

    This will convert it into a link that points to domain.com

    <?php
    /*
    Plugin Name: Custom Widget Title Links
    Plugin URI: http://www.playforward.net/
    Description: Allows you to define a link that is wrapped around widget titles.
    Version: 1.0
    Author: Playforward | Dustin Dempsey
    Author URI: http://www.playforward.net/
    */

    function custom_widget_link( $title ) {

    // assume there's a link attached to the title because it starts with ww, http, or /
    if ( ( substr( $title, 0, 4) == "www." ) || ( substr( $title, 0, 4) == "http" ) || ( substr( $title, 0, 1) == "/" ) ) {

    // split our title in half
    $title_pieces = explode( "|", $title );

    // if there's two pieces
    if ( count( $title_pieces ) == 2 ) {

    // add http if it's just www
    if ( substr( $title, 0, 4) == "www." ) {
    $title_pieces[0] = str_replace( "www.", "http://www.", $title_pieces[0] );
    }

    // create new title from url and extracted title
    $title = '<a href="' . $title_pieces[0] . '" title="' . $title_pieces[1] . '">' . $title_pieces[1] . '</a>';
    }
    }

    return $title;
    }
    add_filter( "widget_title", "custom_widget_link" );

    ?>

  • DavidM
    • DEV MAN’s Mascot

    @jsartwell, thanks too for sharing your solution. I thought to mention to look for the $before_title and $after_title variables, but I’m glad you got that sorted!

    maxaud’s plugin idea might provide a simpler alternative if you’ve got numerous plugins to consider. So you’ve got at least 2 methods to choose from. :slight_smile:

    Cheers,

    David

  • maxaud
    • The Crimson Coder

    Change line # 29 from this:

    $title = '<a href="' . $title_pieces[0] . '" title="' . $title_pieces[1] . '">' . $title_pieces[1] . '</a>';

    To this:

    $title = '<strong><a href="' . $title_pieces[0] . '" title="' . $title_pieces[1] . '">' . $title_pieces[1] . '</a></strong>';

  • DavidM
    • DEV MAN’s Mascot

    Thanks again for posting that @maxaud, your help’s much appreciated!

    Just to mention too, that could also be targeted with CSS using nth-child, like so:

    .widget:nth-child(2) .widget-title {
    font-weight: bold;
    }

    That would add bold weighting to the 3rd widget title. You can substitute the (2) for whatever widget number you need to target.

    Just another method, in case that’s helpful. :slight_smile:

    Cheers,

    David

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.