Support » Plugin: YouTube Channel » Bug: incorrect application of widget_title filter

  • Resolved squarestar

    (@squarestar)


    Hi and thanks for the plugin.

    I’ve come across a bug. On line 37 of /assets/widget.php you use apply_filters on the widget_title, but you don’t include all the arguments for this filter (see https://codex.wordpress.org/Plugin_API/Filter_Reference/widget_title), which breaks any other plugin that depends on information in the second or third arguments.

    For example, I have a plugin that allows me to wrap the title of any widget in an <a></a> element, but it won’t work without access to the second argument, $instance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Aleksandar Urošević

    (@urkekg)

    Hi @squarestar and thank you for reporting this issue.

    Can you please confirm that if you replace line 37 in /wp-content/plugins/youtube-channel/inc/widget.php

    $output .= apply_filters( 'widget_title', $instance['title'] );

    with

    $output .= apply_filters( 'widget_title', $instance['title'], $instance, $wpau_youtube_channel->plugin_slug );

    problem disappear? Can you please tell me what other plugin or custom code you use to wrap widget title to anchors so I can test before releasing bugfix?

    Kind regards,
    Aleksandar

    squarestar

    (@squarestar)

    Hi Aleksandar,

    Yes, that fixes the problem. It’s custom code of my own that I’m using (just 3 functions):

    
    function addTitleHrefFieldToWidgets($widget, $return, $instance)
    {
        // Don't add the field to widgets with no title
        if (empty($widget->get_field_id('title'))) {
            return;
        }
    
        $href = isset($instance['title_link']) ? $instance['title_link'] : '';
    ?>
    
        <p>
        <label>
        Title Link:
        <input type="url" id="<?php echo $widget->get_field_id('title_link'); 
            ?>" name="<?php echo $widget->get_field_name('title_link'); 
            ?>" value="<?php echo $href; ?>" placeholder="https://" >
        </label>
        </p>
    
        <?php
    }
    add_filter('in_widget_form', 'addTitleHrefFieldToWidgets', 10, 3);
    
    function saveWidgetTitleLinkOption($instance, $newInstance)
    {
        if (!empty($newInstance['title_link'])) {
            $instance['title_link'] = $newInstance['title_link'];
        }
    
        return $instance;
    }
    add_filter('widget_update_callback', 'saveWidgetTitleLinkOption', 10, 2);
    
    function hyperlinkWidgetTitle($title, $instance)
    {
        if (empty($title) || empty($instance['title_link'])) {
            return $title;
        }
    
        $title = '<a href="' . $instance['title_link'] . '">' . $title . '</a>';
    
        return $title;
    }
    add_filter('widget_title', 'hyperlinkWidgetTitle', 10, 2);
    

    Thanks!

    Hello @squarestar,

    Thank you very much for this code example, I test and released new version 3.0.11 of plugin with included bugfix, and small feature enhancement regarding nolightbox option for thumbnails view.

    Kind regards,
    Aleksandar

    You’re welcome Aleksandar. Thanks again 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.