WordPress.org

Ready to get started?Download WordPress

Forums

When do I really need remove_filter or remove_action? (2 posts)

  1. SL4YER
    Member
    Posted 1 year ago #

    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:

    <?php
    
    /*
       Plugin Name: WordPress-Plugin Vorlage
       Plugin URI: http://www.iscope.de
       Description: WordPress-Plugin Vorlage
       Author: Fabian Heinrich <fheinrich@iscope.de>
       Author URI: http://www.iscope.de
       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...

  2. Umbercode
    Member
    Posted 1 year ago #

    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 http://codex.wordpress.org/Writing_a_Plugin or http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/ for starters

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.