I completely edited my question.
I use the YITH WooCommerce Compare plugin, I will put the file that contains the codes that are related to my question : https://file.io/StHr7KBJBdxF
$current_cat
variable has been set equal to : public $current_cat = array();
, $this->current_cat
has been used in some parts of the code, How to call the $this->current_cat
outside the file and in the function file?
class YITH_Woocompare_Frontend_Premium extends YITH_Woocompare_Frontend {
…
/**
* The list of current cat inside the comparison table
*
* @since 1.0.0
* @var array
*/
public $current_cat = array();
For example: used a $this->products_list;
, If I want to call it outside the file and in the function file, it goes like this : $products_list = isset( $_COOKIE[ get_cookie_name() ] ) ? json_decode( wp_unslash( $_COOKIE[ get_cookie_name() ] ) ) : array();
I hope you understand what I mean. If this is a beginner’s question, please don’t diss because not everyone is a professional like you. Also, I spent a lot of time solving the problem. It’s not like I want to quickly ask my question here to quickly find an answer.
2
Answers
After much effort, I was able to call
$current_cat
variable outside the plugin and in the function file without the YITH WooCommerce Compare plugin installed.Your
current_cat
is a public property of a class defined by your plugin.Within functions that are part of that same class, code can access that property with
$this->current_cat
. Why?$this
is, inside that class’s code, a reference to the current instance of the class. The->
object operator tells php to look inside$this
.Outside that class’s code you can access public properties like so.
You are trying to do the thing shown in the second line of that example. To do it you need access to the variable returned by
new PluginClass()
in the first line. Different plugins have different ways of making that kind of information available to theme code (functions.php). It’s necessary to look at the plugin’s code to figure out how to do that, or look at the documentation, or consult the plugin’s support forum.With respect, explaining how php classes work is too complex for a Stack Overflow answer. So is explaining how WordPress, plugins, and theme code interact. I hope this answer points you in a useful direction.