More about OOP Theme wrapping code

Hi there,

I desire to understand how to theme wrapper works. I’m want to reveal that I’m not a master in OOP. I know something, but not enough.

Said that, I have some questions:

  1. In this piece of code add_filter('template_include', array('Roots_Wrapping', 'wrap'), 99); what does 'Roots_Wrapping'? It is not a function like 'wrap'

  2. The Roots_Wrapping::$main_template; invoke the $main_template that it is inside of the Roots_Wrapping class, but where is the instance that invoke that class and activates the __construct(...)? i know that the __construct(...) works only if there is a new instance created…


array('Roots_Wrapping', 'wrap')

Is how you specify a class or an instance to resolve the function name. That’s just translated to Roots_Wrapping::wrap() which calls the static wrap function in the Roots_Wrapping class. Without that array syntax it would just look up wrap which doesn’t exist outside of the class.

  1. The answer to this goes back to #1. The wrap function instantiates Roots_Wrapping here Since the class has been instantiated, Roots_Wrapping::$main_template has already been populated. Although note that $main_template is a static variable, meaning you can reference it directly through the class name without an instance of the class.
1 Like

Also please read through this if you haven’t already.

Thanks for answered me.

@Foxaii: yes, I read, but, like I said, I’m not used with OOP, so I would like to have more details for assimilate the OOP coding.

When and how $main_template is populated?

In the wrap() function, how $main stores the value? When the wrap() function is called, through add_filter(), no argument is passed. So, what $main does it for?


You need to read up on filters in WordPress, add_filter and maybe even consider if Roots is right for you.

Once you’ve got to grips with the basics, re-read the wrapper article and things should become clearer.


I’ve read enough about add_filter and apply_filters, but I don’t understand what does it for this pieace of code: $this->templates = apply_filters('roots_wrap_' . $this->slug, $this->templates);

Here is where it is created a new filter -> roots_wrap_base, but if I comment this line, the load still happens. Also, this roots_wrap_base filter hook is not belong to any others file.

Can you explain me?

Never mind! I understood