Built in admin ajax hooks?

时间:2016-12-19 作者:Omer Greenwald

我正在开发一个插件,并使用常规挂钩和过滤器。然而,我现在正试图将我的函数绑定到来自WP的管理ajax调用的响应。

例如,在Plugins Add New screen 我想在插件的ajax安装成功并出现“激活”按钮后运行我的函数。如何在成功的ajax响应中触发我的函数?

使用“plugin_install_action_links“我可以通过php控制操作链接,但ajax的等效元素是什么(如果有)?

1 个回复
最合适的回答,由SO网友:prosti 整理而成

WordPress核心是无法创建自己的挂钩的东西。WordPress中的挂钩是用这些

apply_filters
apply_filters_ref_array
apply_filters_deprecated
do_action
do_action_ref_array
do_action_deprecated
你唯一能做的就是将你的函数注册到某个WordPress核心钩子上。但是这些钩子在哪里?

在一个名为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 ) {
注意,有一个参数@param bool $silent 这是可选的prevent 调用激活挂钩。默认情况下,它设置为false.

在该函数中,您可以找到挂钩:

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 );
再来一个。。。

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 );
还有一个你真正需要的。。。

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 );
我刚刚搜索了钩子生成器函数,如do_action.

你在做什么plugin_install_action_links 是一个filer 吊钩类型。过滤器通常用于replace 某些事情。

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 );
使用“plugin\\u install\\u action\\u links”,我可以通过php控制操作链接,但是ajax的等效元素是什么(如果有)?

有一个文件名为admin-ajax.php. 您可以检查此文件,也可以使用挂钩来执行特定的操作,但目前在中没有定义过滤器admin-ajax.php.

相关推荐

尝试在WordPress中实现AJAX注释,遇到WP错误

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:下面是我对ajax的评论。js文件如下所示: * Let\'s begin with validation functions */ jQuery.extend(jQuery.fn, { /* * check i