0

Hopefully, this is an easy question - I'm just a bit confused.

I've got a custom widget with the usual structure:

class My_Widget extends WP_Widget {
  function My_Widget() {
    // widget actual processes
  }
  function form($instance) {
    // outputs the options form on admin
  }
  function update($new_instance, $old_instance) {
    // processes widget options to be saved
  }
  function widget($args, $instance) {
    // outputs the content of the widget
  }
}
register_widget('My_Widget');

I's like to add my own custom function to it, so I can reuse it. Something like:

function item_width_cols_to_class( $cols_num = null ) {
    if( $cols_num ){
        switch ( $cols_num ){
            case '2':
                return "col140";
                break;
            case '3':
                return "col220";
                break;      
            case '4':
                return "col300";
                break;      
            case '5':
                return "col380";
                break;
            case '6':
                return "col480";
                break;      
            case '7':
                return "col540";
                break;      
            default:
                return "";
                break;
        }
    }
}

However, when I add that code to My_Widget class I got an error saying: "Fatal error: Call to undefined function 'item_width_cols_to_class()'". I'd think it's possible to add custom functions to a widget as it extends the WP_widget class.

Am I doing something wrong? :S

Thanks for help, Dasha

1 Answer 1

1

Where did you call your item_width_cols_to_class "function"?

You are using the function as a method inside the My_Widget class. Therefore, you have to call it with a reference to its non-existent object e.g. $this->item_width_cols_to_class() or self::item_width_cols_to_class() (static).

2
  • yep that's exactly it! I defined the function and am calling it from within My_widget class. I ended up using $this->item_width_cols_to_class(). Still a bit confused with $this (will keep on learning). Thanks a lot for super fast answer :)
    – dashaluna
    Jun 3, 2011 at 9:29
  • 2
    @dashaluna: I'm trying to explain that for you (attetion: bad english skills incoming). You're going to create a new object of your My_Widget class somewhere in your code (WP will do that for you). Think of this snippet $obj = new My_Widget();. $obj has now a reference to this object. Everytime you want to access a class-member, you could call $obj->yourMethod(). This only works, because you've created an object with new. Inside your class, you want to do something without the need of creating a object of its own. There you can use the $this reference, which points to its own.
    – Roman
    Jun 3, 2011 at 9:50

Your Answer

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

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