Deleting subscriber when deleting user

I am trying to automatically delete a subscriber, whenever a user is deleted, i tried to hook into the delete_user action and wpmu_delete_user action and then use the cancel subscriber function from the subscriber class.
here is the edited code in subscribe-by-email.php

public function __construct() {
		self::$time_between_batches = apply_filters( 'sbe_time_between_batches', 1200 );

		$this->set_globals();
		$this->includes();

		$this->model = incsub_sbe_get_model();

		add_action( 'init', array( &$this, 'init_plugin' ), 1 );

		add_action( 'init', array( &$this, 'confirm_subscription' ), 2 );
		add_action( 'init', array( &$this, 'cancel_subscription' ), 20 );
        add_action( 'delete_user', 'my_delete_user' );
        add_action( 'wpmu_delete_user', 'my_delete_user' );

		if ( ! get_transient( 'incsub_sbe_updating' ) ) {
			add_action( 'transition_post_status', array( &$this, 'process_instant_subscriptions' ), 2, 3);
			add_action( 'wp_loaded', array( &$this, 'process_scheduled_subscriptions' ) );
			add_action( 'init', array( &$this, 'maybe_delete_logs' ) );
		}

		register_activation_hook( __FILE__, array( &$this, 'activate' ) );
		register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) );

		add_action( 'widgets_init', array( &$this, 'widget_init' ) );

		add_action( 'plugins_loaded', array( &$this, 'load_text_domain' ) );

		add_action( 'wpmu_drop_tables', array( &$this, 'uninstall' ) );
	}
    public function my_delete_user( $user_id ) {
	global $wpdb;

        $user_obj = get_userdata( $user_id );
        $email = $user_obj->user_email;
        incsub_sbe_cancel_subscription($email);

    }

Am i on the right track? currently the subscriber is not deleted when the user is :slight_frown: It is a multisite im working on

  • Lindeni Mahlalela

    Hello Marie,

    I hope you are doing great today. I am sorry for the delayed response from our side and thank you very much for your patience so far.

    You are on the right track but only one small thing that will make your code not work. As your 'my_delete_user($user_id)' is defined as a member function of the class 'Incsub_Subscribe_By_Email' you should call this function with reference to the class like all the other hooks are used within the same constructor.

    Just change the lines:

    add_action( 'delete_user', 'my_delete_user' );
    add_action( 'wpmu_delete_user', 'my_delete_user');

    To this:

    add_action( 'delete_user', array( &$this, 'my_delete_user') );
    add_action( 'wpmu_delete_user', array( &$this,'my_delete_user') );

    That should do it. This tells PHP that it is calling a function within the current class. If it is not done this way then PHP will throw a function does not exist error.

    Now your final code should look like this:

    public function __construct() {
    	self::$time_between_batches = apply_filters( 'sbe_time_between_batches', 1200 );
    
    	$this->set_globals();
    	$this->includes();
    
    	$this->model = incsub_sbe_get_model();
    
    	add_action( 'init', array( &$this, 'init_plugin' ), 1 );
    
    	add_action( 'init', array( &$this, 'confirm_subscription' ), 2 );
    	add_action( 'init', array( &$this, 'cancel_subscription' ), 20 );
    
    	add_action( 'delete_user', array( &$this, 'my_delete_user') );
    	add_action( 'wpmu_delete_user', array( &$this,'my_delete_user') );
    
    	if ( ! get_transient( 'incsub_sbe_updating' ) ) {
    		add_action( 'transition_post_status', array( &$this, 'process_instant_subscriptions' ), 2, 3);
    		add_action( 'wp_loaded', array( &$this, 'process_scheduled_subscriptions' ) );
    		add_action( 'init', array( &$this, 'maybe_delete_logs' ) );
    	}
    
    	register_activation_hook( __FILE__, array( &$this, 'activate' ) );
    	register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) );
    
    	add_action( 'widgets_init', array( &$this, 'widget_init' ) );
    
    	add_action( 'plugins_loaded', array( &$this, 'load_text_domain' ) );
    
    	add_action( 'wpmu_drop_tables', array( &$this, 'uninstall' ) );
    }
    
    public function my_delete_user( $user_id ) {
    	global $wpdb;
    
    	$user_obj = get_userdata( $user_id );
    	$email = $user_obj->user_email;
    	incsub_sbe_cancel_subscription($email);
    }

    I hope this helps you achieve your goals. Thank you once again for waiting and please enjoy your day further.

    Cheers,
    Mahlamusa

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.