DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.67k stars 2.25k forks source link

Global Variables? #270

Closed neelakansha85 closed 9 years ago

neelakansha85 commented 9 years ago

Hi Tom,

Thanks for your hardwork. I really appreciate it. I am a beginner for wordpress plugin development and am currently trying to implement my half baked plugin using your boilerplate. I will appreciate if you can help me identify where in this boilerplate should I be writing or putting down global variables that I would want to use in many different classes and methods?

Would you suggest creating a separate file in the includes dir or declaring them in includes/class-plugin-name.php as best practices?

Thanks, Neel

tommcfarlin commented 9 years ago

Hey @neelakansha85,

Technically, because of the object-oriented nature of the Boilerplate, you shouldn't have anything in a global variable. Instead, you should have the information in a class that's shared among both the dashboard and the public-facing side of the site.

From there, you then call into the instance of that class to grab the data that you're looking for. This may be the class you're looking for since it's meant to be shared between the two.

Other options would be to create your own class, then inject it into each class that needs them via their constructors, and them access them via methods in the class.

Hope this helps!