Support » Plugins » Hacks » When do I really need remove_filter or remove_action?

  • Hi!

    I read a few tutorials that suggest you have to clean up your wordpress plugin (when deactivating) by removing all filters and actions that you have set. Is that right? I found nothing like that in other plugin-code… So when do I really need these functions?

    This is my code until now:

       Plugin Name: WordPress-Plugin Vorlage
       Plugin URI:
       Description: WordPress-Plugin Vorlage
       Author: Fabian Heinrich <>
       Author URI:
       Version: 0.0.1
    if(!class_exists('myPlugin')) {
       class myPlugin {
          function __construct() {
             register_activation_hook( __FILE__, array(&$this, 'activate'));
             register_deactivation_hook( __FILE__, array(&$this, 'deactivate'));
          function activate() {
             // hier setzen wir unsere Hooks, z.B.:
             add_action('edit_category_form', array(&$this, 'myHookCategory'));
          function deactivate() {
             // hier räumen wir wieder auf, wenn das Plugin deaktiviert wird, z.B.:
             remove_action('edit_category_form', array(&$this, 'myHookCategory'));
          function myHookCategory() {
             // hier kommt der Quelltext, den wir ins System bringen wollen
             echo '<h3>Mein erster Hook, yay!</h3>';
    } else {
       $myPluginObject = new myPlugin();
       if (isset($myPluginObject)) {
          add_action('init', array(&$myPluginObject, 'activate'));

    P.S.: Does someone know, why there is an add_action added after initializing the object? With initializing it, the constructor is called, isn’t it? So I dont understand that part…

Viewing 1 replies (of 1 total)
  • Unless I completely misunderstand, this code makes no sense to me.
    add_action does not add an action permanently but only for the current instance, so you do not need the remove_action in this case.
    Also, the “else” clause will only be called when there is another plugin which defines the myPlugin class. I am guessing you think that that part of the code gets executed after activation? WHich is not the case, however even if it did work like that then there would be no need to call the activate function since the plugin is already activated (but again, it doesn’t work like that).
    I am afraid you have been reading the wrong tutorials because this code makes no sense at all.
    Try or for starters

Viewing 1 replies (of 1 total)
  • The topic ‘When do I really need remove_filter or remove_action?’ is closed to new replies.