Ready to get started?Download WordPress


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

  1. SL4YER
    Posted 1 year ago #


    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: 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() {
          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.:
          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
    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


No tags yet.