Remove action from plugin on other plugin

I’m trying to remove two of the actions that adds a plugin (sportspress specifically). The actions of this plugins are:

add_action('sportspress_before_single_player','sportspress_output_player_details', 15);
add_action('sportspress_single_player_content','sportspress_output_player_statistics',20);

I’ve created a plugin, and I want to remove these hooks, this is my code:

<?php
/*
Plugin Name: my plugin

Description: Plugin to override plugins' hooks
Version: 0.1
Author: Company Name
Author URI: http://www.example.com/
License: GPL2
*/


add_action('plugins_loaded','remove_hooks');
function remove_hooks(){
    remove_action( 'sportspress_before_single_player', 'sportspress_output_player_details' );
    remove_action( 'sportspress_single_player_content', 'sportspress_output_player_statistics' );
}

I’ve searched and tried a lot of things, but I can’t make it work.

3 s
3

There are two things that confuse people when trying to remove a hook:

  1. The remove_action() or remove_filter() calls must take place after the add_action() or add_filter() calls, and before the hook is actually fired. This means that you must know the hook when the calls were added and when the hook is fired.
  2. The remove_action() or remove_filter() calls must have the same priority as the add_action() or add_filter() call

If these hooks were added on the init filter at the default priority, then to remove them, we’d simply hook into init at a priority later than 10 and remove them.

add_action( 'init', 'wpse_106269_remove_hooks', 11 );
function wpse_106269_remove_hooks(){
    remove_action( 'sportspress_before_single_player', 'sportspress_output_player_details', 15 );
    remove_action( 'sportspress_single_player_content', 'sportspress_output_player_statistics', 20 );
}

From https://codex.wordpress.org/Function_Reference/remove_action

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

Leave a Comment