1

Based on this question, I'm getting an error Warning: Missing argument 2 for....

This happens for some widgets those are created by plugins.

-- Edit 1 --

Here's a simple widget that causes the error:

function my_new_widget_register() {
    register_widget( 'my_new_widget' );
}
add_action('widgets_init', 'my_new_widget_register');

class my_new_widget extends WP_Widget {

    function my_new_widget() {
        // Instantiate the parent object
        parent::__construct( false, 'my_new_widget' );
    }

    function widget( $args, $instance ) {
        // Widget output
            extract( $args );
            $title = apply_filters('widget_title', $instance['title']);
            echo $before_widget;
            echo $title;
            echo $after_widget;
    }
}

The error is clearly caused by $title.

6
  • which plugins exactly? have you contacted the plugins' developers?
    – Michael
    Jun 11, 2015 at 11:28
  • wp-polls for example, I think the problem is with my code. Like there's something missing in this function which makes it outputs errors: function remove_widget_title($title, $instance) { if (isset($instance['title']) && empty($instance['title'])) { $title = ''; } return $title; } add_filter('widget_title','remove_widget_title', 10, 2);
    – Ahmed Saad
    Jun 11, 2015 at 11:48
  • 1
    Please edit your question and paste in the relevant info. It's a lot easier to read code in the question than in the comments, for one thing.
    – Pat J
    Jun 11, 2015 at 14:43
  • Find a link to the wp-polls widget code please.
    – s_ha_dum
    Jun 11, 2015 at 17:47
  • Updated my question with a simple widget causing the error mentioned.
    – Ahmed Saad
    Jun 11, 2015 at 17:53

2 Answers 2

3

You don’t pass more than argument to that filter, so any callback expecting more than one will not get it. The core calls this filter always like this:

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

But you are passing just $instance['title']. Add the missing parameters, and the error will vanish.

If you cannot change the widget, change the registration for the callback for the filter, and make the other arguments optional by setting a default value:

add_filter( 'widget_title', 'my_widget_title_filter', 10, 3 );

function my_widget_title_filter( $title, $instance = [], $id_base = '' ) {
    if ( '' === $id_base )
        return $title;

    // do something with the title, then
    return $title;
}
5
  • I can't make any changes to the widgets, especially this line: $title = apply_filters('widget_title', $instance['title']); Because all widgets are using this filter like that. I've to make the change through add_filter('widget_title','my_function');
    – Ahmed Saad
    Jun 13, 2015 at 23:44
  • @AhmedSaad see my update.
    – fuxia
    Jun 14, 2015 at 0:02
  • This doesn't solve my problem. I need use both $title and $instance['title'] parameters in the widget_title filter. What I want to achieve exactly is to eliminate automatically generated $title for some widgets. See this reply: wordpress.stackexchange.com/questions/191120/…
    – Ahmed Saad
    Jun 14, 2015 at 0:58
  • You cannot use data that is not passed from the filter call. This is impossible, like drinking from an empty glass.
    – fuxia
    Jun 14, 2015 at 1:28
  • function remove_widget_title($title, $instance) {if (isset($instance['title']) && empty($instance['title'])) {$title = '';} return $title;} add_filter('widget_title','remove_widget_title', 10, 2); This function works just fine except that the missing argument error shows up, which is weird. Anyway what I want to achieve at the end is to wrap the widget contents with a wrapper only in case the widget doesn't have $title, either inserted or automatically added.
    – Ahmed Saad
    Jun 14, 2015 at 1:33
0

The Missing Arguments error disappeared after making a small change to the parameters in the function:

function remove_widget_title($title, $instance)

to:

function remove_widget_title($title, $instance = [])

Your Answer

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

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