0

I'm new to WordPress plugin development and am using the boilerplate. I am having problems with passing parameters around. In the boiler plate function define_public_hooks I have:

$poster = new RSS_To_Post_Poster();
        $this->loader->add_action( 'activate_plugin', $poster , 'set_jobs', 10, 1);
        $this->loader->add_action( 'deactivate_plugin', $poster , 'clear_jobs', 10, 1);

I instantiate a class RSS_To_Post_Poster and add two hooks:

One on event activate_plugin and one on event deactivate_plugin

On activate_plugin set_jobs is called. This among other things loads a list of file names from a directory into an array held in the class RSS_To_Post_Poster.

When the plugin is deactivated clear_jobs is called and this needs to clear jobs previously set and use the list of file names previously read in during the set_jobs call.

The problem: The filenames I previously read into the array in the set_jobs no longer exists when I call clear_jobs.

My question is: 1. Is the class I instantiate

$poster = new RSS_To_Post_Poster();

NOT the same class used when the actions are executed?

$this->loader->add_action( 'activate_plugin', $poster , 'set_jobs', 10, 1);
$this->loader->add_action( 'deactivate_plugin', $poster , 'clear_jobs', 10, 1);

I presume not since variables I set in set_jobs are not set when the calls to clear_jobs is made. I'm clearly missing some understanding of hos this works.

  1. Is there a way to pass parameters from one action to the other?

Advanced thanks.

  • 1
    Variables and class instances are not persistent across multiple requests in PHP or WordPress. So you're taking the wrong approach to what you're doing. If you want to save data when the plugin is activated, and clear it when the plugin is deactivated, then you need to store that data in the database. – Jacob Peattie Mar 2 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.