I have a plugin that does some initialization stuff using register_activation_hook
called from its __construct()
.
I’ve since extended my plugin to add functionality to it. For the sake of organization, I’ve moved the extended functions to it’s own class. I need to create a table that will deal with data unique to this new class.
My new class is instantiated from the construct of my main plugin. And from the construct
of my new class, I am trying to call register_activation_hook
a second time to create my table. Not surprisingly, this does not seem to work. (I have tested my db creation code elsewhere and it does work).
Is there a way that I can hook into the register_activation_hook
from my new class to ensure the database is created at activation, or maybe even piggyback onto the parent class somehow to make sure the db is created?
Use some kind of a Controller and combine the calls to both classes in one callback for each action. The controller should be responsible for the real assignation of business logic to an event (action), not some code outside of your classes.
The basic point is: Your plugin controller should not alter data, only Models should do that.
The following example shows how to create a table (I use an option here to keep things simple) and an option:
You can test the success with a second plugin:
Some links for further reading: