I'm working on creating a plugin out of some custom code I use a lot, in hopes that other people find it useful too.

It works, so yay! but...

Here's my question:

I feel like I'm missing some puzzle pieces in my understanding of the system.

What happens to all the code I add and hook everywhere after I deactivate my plugin?

In my main .php file, when is the code that's not in a function being executed? Only at install? Then what is the purpose of register_activation_hook( __FILE__,'install_method'); ?

If I put add_action and add_filter in my main php file, do I have to remove them at uninstall? (For example, I know that if I add_option('option', '0', '', 'yes'); during install I have to delete_option('option'); at uninstall.)

In some tutorial, they told me to put if ( is_admin() ) {} around my code, is this necessary?


filters and actions are added with every request. when you disable a plugin, they immediately disappear, because they only exist in your plugin code. this is how you're able to selectively add them on a per-request basis. you only need to remove filters or actions if you don't want them to run in the current request.

is_admin() is a check if the admin pages are being displayed, and should wrap things only for admin pages.


what is the purpose of register_activation_hook

The Purpose of register_activation_hook is to let your function run code when the plugin is activated for example if your plugin uses a custom database table you can use that hook to call a function that will create that table in the database, or if you want to define some default options using add_option.

Now you don't have to delete the options when the plugin is deactivated because some times people deactivate a plugin temporarily and they would have to redefine the plugins settings every time they do so, instead you should provide an option to "uninstall" the plugins options using a custom function which you can run.

as for add_action and add_filter that are in your main.php file, you don't need to worry about them when the plugin is activated they wont get processed so there is no need to remove them.

and lastly is_admin() like Milo said, it's a conditional function which checks if you are currently on an admin (WordPress backend) page, so you could call your functions or hooks only on admin pages.

  • wish I could select 2 answers, thanks for clearing this up! – joon Jul 13 '11 at 22:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.