• I’m trying to create a basic widget. I’m using a php class and when using $this->varName; it’s giving me an error like it doesn’t know it’s in an object. Here is the error:
    Fatal error: Using $this when not in object context in /Applications/MAMP/htdocs/wordpress1/wp-content/plugins/mytest/mytest.php on line 30

    Thanks for any help.
    Here is the code:
    <?php
    /*
    Plugin Name: mytest
    Plugin URI: https://www.mysite.com
    Version: v1.00
    Author: Eric
    Description: does a test

    */
    error_reporting(E_ALL);

    class mytestClass {

    public $lastChecked;

    public function __construct() {

    $this->lastChecked=date(‘n-j-Y g:i:s’, time());

    }

    public function control(){
    echo ‘I am a control panel’;
    }

    public function widget($args){
    echo $args[‘before_widget’];
    echo $args[‘before_title’] . ‘Test Widget’ . $args[‘after_title’];
    echo ‘I am your widget’;
    echo $this->lastChecked;
    echo $args[‘after_widget’];
    }

    public function register(){
    register_sidebar_widget(‘mytestClass’, array(‘mytestClass’, ‘widget’));
    register_widget_control(‘mytestClass’, array(‘mytestClass’, ‘control’));
    }

    }

    $mytestClass=new mytestClass();
    add_action(“widgets_init”, array($mytestClass, ‘register’));

    ?>

Viewing 1 replies (of 1 total)
  • Thread Starter merlinti

    (@merlinti)

    I figured it out by looking at another plugin that uses a class.
    It looks like I needed to pass the object by reference.

    I changed these 2 lines:

    register_sidebar_widget(‘mytestClass’, array(‘mytestClass’, ‘widget’));
    register_widget_control(‘mytestClass’, array(‘mytestClass’, ‘control’));

    TO

    register_sidebar_widget(‘GoogleWeather’, array(&$this, ‘widget’));
    register_widget_control(‘GoogleWeather’, array(&$this, ‘control’));

Viewing 1 replies (of 1 total)
  • The topic ‘Using a class in a plugin using $this->’ is closed to new replies.