Question

To be specific, I don't really know what I am doing, but I'm trying to reference $this from another file.

I'm working with Wordpress(subscribe2 plugin) and trying to get the function of subscribing to specific categories from the plugin and putting it into my own signup form.

The ability to do this is already in the settings for the plugin, so I'd like to put them in my own form, but they look like this:

$this->get_usermeta_keyname('s2_subscribed')

so my question: How do I reference "$this" in my own file? I have been googling it all morning, but all of my results see "$this" as "this" and don't show me what I want.

Was it helpful?

Solution

$this refers to the current class context. So if you have a class named MyClass, within that class you can access functions and elements using $this. Outside of that class, $this has no context and won't work, but if you have instantiated the class and if the functions or variables are public, then you can access them via a reference to the class.

Example:

<?php
Class MyClass {
    $class_var = "Class Var Value";
    function class_func_1() {
        print $this->class_var;
        $this->class_func_2();
    }

    function class_func_2() {
        print "Class Func 2";
    }
}
?>

So the dummy class above uses $this to reference its own elements. But outside the class you cannot reference them with this. But if we instantiate an instance of the class, we can access them by the reference to the instance of the class:

<?php
print $this->class_var;  // Fails miserably
$this->class_func_1();   // Also fails

$class_instance = new MyClass();
print $class_instance->class_var; // Var access works
$class_instance->class_func_1();  // Call method works
?>

Since this is a wordpress function, I'm curious if you are trying to access something in a plugin (which might use a class) or possibly something in a purchased theme. Either way, there should be a reference to the instance of the class somewhere in the file. Once you have this variable, then you can access the actions and contents of the class.

With more information, I think we can probably provide better direction...


Update:

Ok, it appears you are using the subscribe2 plugin. At the bottom of the plugin file (subscribe2.php) is this line:

$mysubscribe2 = new subscribe2();

What this is doing is creating a global variable that points to an instance of the class. So instead of using $this->method_name() you could use $mysubscribe2->method_name().

However, looking at the file you posted - it almost looks like you grabbed a portion of the plugin code and just put it into a template. I'm not sure what you are trying to accomplish but from here it appears you are heading down a rough path...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top