2

I'm developing a plugin and using regular hooks and filters. However, I'm now trying to bind my function to responses from WP's admin ajax calls.

For example, in the Plugins Add New screen I would like to run my function after ajax installation of a plugin is complete successfully and the "Activate" button appears. How can I trigger my function on successfull ajax response there?

Using "plugin_install_action_links" I can control the action links through php, but what is the ajax equivelent (if there is one)?

  • Is this the activation of your plugin or the activation of other plugins? – Tom J Nowell Dec 19 '16 at 13:07
  • It's actually not activation, but rather installation of other plugins. but it can also be binding to ajax activation of other plugins, this will also help. – EranG Dec 19 '16 at 13:09
  • and you've asked how to do the generic non-WP case of intercepting an AJAX request made by jQuery in javascript? – Tom J Nowell Dec 19 '16 at 15:03
  • Not non-WP case. I'm looking for a WP hook for ajax. – EranG Dec 19 '16 at 18:19
2
+50

WordPress core is something where you cannot create your own hooks. Hooks in WordPress are created with these

apply_filters
apply_filters_ref_array
apply_filters_deprecated
do_action
do_action_ref_array
do_action_deprecated

The only thing you can do is to register your function to certain WordPress core hook. But where are these hooks?

You may expect these hooks in a function called activate_plugin.

File: wp-admin/includes/plugin.php
512: /**
513:  * Attempts activation of plugin in a "sandbox" and redirects on success.
514:  *
515:  * A plugin that is already activated will not attempt to be activated again.
516:  *
517:  * The way it works is by setting the redirection to the error before trying to
518:  * include the plugin file. If the plugin fails, then the redirection will not
519:  * be overwritten with the success message. Also, the options will not be
520:  * updated and the activation hook will not be called on plugin error.
521:  *
522:  * It should be noted that in no way the below code will actually prevent errors
523:  * within the file. The code should not be used elsewhere to replicate the
524:  * "sandbox", which uses redirection to work.
525:  * {@source 13 1}
526:  *
527:  * If any errors are found or text is outputted, then it will be captured to
528:  * ensure that the success redirection will update the error redirection.
529:  *
530:  * @since 2.5.0
531:  *
532:  * @param string $plugin       Plugin path to main plugin file with plugin data.
533:  * @param string $redirect     Optional. URL to redirect to.
534:  * @param bool   $network_wide Optional. Whether to enable the plugin for all sites in the network
535:  *                             or just the current site. Multisite only. Default false.
536:  * @param bool   $silent       Optional. Whether to prevent calling activation hooks. Default false.
537:  * @return WP_Error|null WP_Error on invalid file or null on success.
538:  */
539: function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {

Note there is a parameter @param bool $silent that is optional that will prevent calling activation hooks. By default it is set to false.

In that function at the moment, you can find hooks:

File: wp-admin/includes/plugin.php
563:        if ( ! $silent ) {
564:            /**
565:             * Fires before a plugin is activated.
566:             *
567:             * If a plugin is silently activated (such as during an update),
568:             * this hook does not fire.
569:             *
570:             * @since 2.9.0
571:             *
572:             * @param string $plugin       Plugin path to main plugin file with plugin data.
573:             * @param bool   $network_wide Whether to enable the plugin for all sites in the network
574:             *                             or just the current site. Multisite only. Default is false.
575:             */
576:            do_action( 'activate_plugin', $plugin, $network_wide );

One more...

File: wp-admin/includes/plugin.php
563:        if ( ! $silent ) {
...
578:            /**
579:             * Fires as a specific plugin is being activated.
580:             *
581:             * This hook is the "activation" hook used internally by register_activation_hook().
582:             * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
583:             *
584:             * If a plugin is silently activated (such as during an update), this hook does not fire.
585:             *
586:             * @since 2.0.0
587:             *
588:             * @param bool $network_wide Whether to enable the plugin for all sites in the network
589:             *                           or just the current site. Multisite only. Default is false.
590:             */
591:            do_action( "activate_{$plugin}", $network_wide );

And one you actually need...

File: wp-admin/includes/plugin.php
605:        if ( ! $silent ) {
606:            /**
607:             * Fires after a plugin has been activated.
608:             *
609:             * If a plugin is silently activated (such as during an update),
610:             * this hook does not fire.
611:             *
612:             * @since 2.9.0
613:             *
614:             * @param string $plugin       Plugin path to main plugin file with plugin data.
615:             * @param bool   $network_wide Whether to enable the plugin for all sites in the network
616:             *                             or just the current site. Multisite only. Default is false.
617:             */
618:            do_action( 'activated_plugin', $plugin, $network_wide );

I just searched for the hook generator functions such as do_action.


What you do plugin_install_action_links is a filer type of hook. Filter are typically there to replace certain things.

File: wp-admin/includes/class-wp-plugin-install-list-table.php
519:            /**
520:             * Filters the install action links for a plugin.
521:             *
522:             * @since 2.7.0
523:             *
524:             * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
525:             * @param array $plugin       The plugin currently being listed.
526:             */
527:            $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );

Using "plugin_install_action_links" I can control the action links through php, but what is the ajax equivelent (if there is one)?

There is a file called admin-ajax.php. You can examine this file, and you can use hooks to do your specific things, but there are no filters defined at the moment in admin-ajax.php.

  • Thanks. When searching for a way to connect an action to plugin install (not activate), I saw two action hooks in plugin-install.php. one is "install_plugins_{$tab}" and the other is "install_plugins_pre_{$tab}". Could you tell me what exactly is "tab" referring to in this case? How can I associate my function to the ajax installation in this case? – EranG Dec 25 '16 at 10:00
  • I will try to answer you in here wordpress.stackexchange.com/questions/250326/… – prosti Dec 25 '16 at 12:36

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.