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

Best Practice when adding action #398

Open yehiasalam opened 8 years ago

yehiasalam commented 8 years ago

Hi,

This boilerplate is great and giving me a head start , and I want to ask something regarding the best practices. I'm creating a separate class for each admin page. so I have for example class-myplugin-admin-jobs.php in my admin folder.

Now from what i see in the class-myplugin.php we are adding all the actions using the loader, and I was wondering is it a best practice to add all the actions there, or in the construct function in my separate adminclasses. The construct function makes more sense since the class will be responsible for injecting its own actions and classes.

143markr commented 8 years ago

Thanks for this boilerplate.

I'm also having trouble with the loader class. I'm attempting to follow a tutorial on implementing the settings api in a plugin with OOP priniciples (https://tommcfarlin.com/defining-an-interface-for-the-wordpress-settings-api/) and they, and almost everyone else online it seems, want to load their hooks in the construct.

I have no problem with the loader. But, would you please explain to an OOP noob how to alter existing classes that happen to load hooks in their constructs, so that they may instead be used with this loader class?

DevinVinson commented 8 years ago

Putting your actions in the construct is not best practice and Tom definitely would not be recommending it considering he has been digging into testing lately.

You can simply load you hooks into the loader like in the boiler plate...or don't. There is no requirement if you don't like it and find it easier to build without it.

yehiasalam commented 8 years ago

@DevinVinson but why its not a best practice? putting it in the construct will make the class self contained, creating its own hooks

webmarka commented 8 years ago

Since every actions and filters are registered soon in the main plugin class (it's executed in the constructor), I think more clarifications about the loader class and maybe some more code development for providing ways to alter/remove hooks would be welcome! +1