0

I want to extend an existing plugin by writing my own. I think I instantiated it correctly because it appears on the plugin list in the dashboard (although I found it strange that I didn't need to write activation or deactivation code...).

I tested my plugin by using a simple echo 'fd is running'; which strangely shows up at the top of everything - every page of the website and even in the admin dashboard. So I think there is definitely something wrong there.

But I want to specifically extend another plugin. So I used an action hook that the author made in his main file's construct:

existing plugin

public static function instance() {

            if ( ! isset( self::$instance ) && ! (self::$instance instanceof self) ) {
                self::$instance = new self();
                self::$instance->setup_constants();
                self::$instance->actions = array();
                self::$instance->filters = array();

                add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );

                add_action( 'bp_loaded', array( self::$instance, 'bp_include' ) );

                global $ap_classes;
                $ap_classes = array();

                self::$instance->includes();

                self::$instance->ajax_hooks();
                self::$instance->site_include();

                self::$instance->anspress_forms         = new AnsPress_Process_Form();
                self::$instance->anspress_query_filter  = new AnsPress_Query_Filter();
                self::$instance->anspress_cpt           = new AnsPress_PostTypes();
                self::$instance->anspress_reputation    = new AP_Reputation();

                /*
                 * ACTION: anspress_loaded
                 * Hooks for extension to load their codes after AnsPress is leaded
                 */
                do_action( 'anspress_loaded' );

                self::$instance->setup_hooks();
            }

            return self::$instance;
        }

my plugin

function find_do_for_anspress() {
    $FDClassStart = new Find_Do_For_AnsPress();
}
add_action( 'anspress_loaded', 'find_do_for_anspress' );

However, I wanted to test to see if my code was even using that action hook, so at the bottom of the existing plugin I used has_action(); to try and see if I was using it correctly:

existing plugin

//...all of his other code is above. This is after he closes off his Class code and any other functions he added at the bottom, including uninstallation code...

 if(has_action('anspress_loaded', 'find_do_for_anspress')){
    echo 'fd is hooked';
} else {
    echo 'NOT WORKING CORRECTLY';
}
0

... (although I found it strange that I didn't need to write activation or deactivation code...).

You don't need activation of deactivation code unless there is something you need to do on activation or deactivation.

I tested my plugin by using a simple echo 'fd is running'; which strangely shows up at the top of everything - every page of the website and even in the admin dashboard.

Plugins initialize prior to other content and your code echos data during that initialization. This isn't surprising at all. It is exactly what should be expected.

Yes, that is how you use an action hook. That particular one doesn't pass any variables so it is very simple.

Do I need to instantiate my code as a "plugin" per se? Isn't it enough to just put some PHP files in a folder in the same directory where all the plugins are located and have them run off action/filter hooks? Or for any sort of code to work does it need to be either a theme or plugin?

You need to put your code somewhere that will execute. That is 1) a theme, 2) a plugin 3) a mu-plugin. The first two are most common.

1) You can drop code into your theme's functions.php but shouldn't unless you are the author of the theme as it will be overwritten when the theme updates.

2) No, you can't just put code in the plugins directory. It won't execute. You need to add the appropriate plugin headers and activate the plugin via the backend interface.

3) You can drop code into a mu-plugin file and it will just execute. This is a powerful option as the code will run for all themes and all blogs (if a network).

I'm not sure what other questions you have.

  • Your answer explains a lot, but I'm still confused as to why the has_action() code I'm using returns that there is nothing being loaded on that action hook. That's my main question really. – whatwhatwhat Nov 22 '15 at 22:18
  • Do I need to instantiate my code as a "plugin" per se? Isn't it enough to just put some PHP files in a folder in the same directory where all the plugins are located and have them run off action/filter hooks? Or for any sort of code to work does it need to be either a theme or plugin? I just want to add some pages that will load after some other pages created by the existing plugin. – whatwhatwhat Nov 22 '15 at 22:28
  • Figured it out - I was only declaring the function find_do_for_anspress but never actually using it....sorry...rookie mistake but I'm brand new to PHP :) I'll mark your answer as correct though because all the reading I did helped me to keep asking questions that eventually led me to the answer :) – whatwhatwhat Nov 23 '15 at 1:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.