1

I am writing my own plugin to customize a public plugin. My plugin is doing this customization by removing an action via remove_action() and adding it back with my customized code. The code below illustrates just the removal part in a testable way. Obviously both plugins need to be active to illustrate the problem.

First, the original plugin:

<?php
/*
  Plugin Name: Sample_Plugin
  Version: 1.0.0
 * 
 */

class Sample_Plugin {
    function __construct() {
        add_action('admin_init', array($this, 'admin_init'));
    }

    public function admin_init() {
        add_action('admin_head', array($this, 'js_vars'));
    }

    public function js_vars() {
        ?>
        <script type="text/javascript">
            // AJAX link used for the autosuggest
            var sample_ajax_suggest_link = <?php
        echo wp_json_encode(
                add_query_arg(
                        array(
                            'action' => 'sample_ajax_suggest',
                            'post_type' => rawurlencode(get_post_type()),
                        ),
                        wp_nonce_url('admin-ajax.php', 'sample-plugin')
                )
        );
        ?>;
        </script>
        <?php
    }
}    
global $sample_plugin;
$sample_plugin = new Sample_Plugin();

My plugin to modify the plugin above:

<?php
/*
  Plugin Name: CUSTOMIZE the Sample_Plugin
  Version: 1.0.0
 * 
 */

global $sample_plugin;

$result = remove_action('admin_head', array($sample_plugin, 'js_vars'));

if($result){
    $result = "Yes, it was.";
}else{
    $result = "No, it wasn't.";
}
error_log("Was the action removed? " . $result);

Why is remove_action in the "CUSTOMIZE" plugin returning false? I've tried changing the priority but I get the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.