It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. I've created a plugin, and of course being me, I wanted to go with a nice OO approach. Now what I've been doing is to create this class and then just below create an instance of this class:. And similarly I found the a few people using following hook:. What is generally considered the best way to create a WP class instance on load and have this as a globally accessibly variable? Perhaps someone could enlighten me on this point. Edit: Just to confuse matters, I've also seen this used although I wouldn't use this method myself because turning a nicely OO class into a function seems to defeat the point of it :.
Using a custom constructor
But how is it possible to avoid global variables?
Want to learn object-oriented programmming? Get my free course. But generating HTML inside a function or method is messy. The common cause of that is adding conditional logic into the HTML code. This increases the complexity of the code and reduces its readability. After all, PHP is also a templating language. Of course, we have themes that mix the two a lot. But using object-oriented programming with themes is a complex topic that deserves more than one article. This is exactly the type of problem we want to solve with the class that we want to design. But you also love comments!
Shortcode function explained
This function gives the body element different classes and can be added, typically, in the header. Expand full source code Collapse full source code View on Trac. For example, to add a unique class to the same template used above:. To add the following to the WordPress Theme functions. The above example about how to remove inline classes via filters is incorrect. Here is the correct way to do it:.
The class names are many. Expand full source code Collapse full source code View on Trac. If you are writing custom themes and are using this function, make sure to pass the first parameter, although it might be empty. Expand full source code Collapse full source code. You must log in before being able to contribute a note or feedback. Skip to note 1 content If you are writing custom themes and are using this function, make sure to pass the first parameter, although it might be empty. With Params Passing an array of your own classes. Link Text.