WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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";
            case '3':
                return "col220";
            case '4':
                return "col300";
            case '5':
                return "col380";
            case '6':
                return "col480";
            case '7':
                return "col540";
                return "";

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

share|improve this question
up vote 1 down vote accepted

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

share|improve this answer
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 '11 at 9:29
@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. – rofflox Jun 3 '11 at 9:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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