WordPress插件中的is_admin()和DOING_AJAX [英] is_admin() and DOING_AJAX in Wordpress Plugins

查看:64
本文介绍了WordPress插件中的is_admin()和DOING_AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Wordpress插件,其中插件的主文件包括一个PHP文件,具体取决于(至少应该是)后端还是前端.

Im developing a Wordpress-plugin, where the main file of the plugins includes a PHP-file depending on (supposed to at least) if you are back-end or front-end.

由于is_admin()在AJAX请求上返回true,因此我使用了DOING_AJAX常量来检查AJAX是否完成:

As the is_admin() returns true on AJAX requests, I have used the DOING_AJAX constant to check whetever AJAX is done or not:

if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
    require_once('admin/functions_admin.php'); 
 }
 else { 
    require_once('public/functions_public.php'); 
 }

正确的文件已加载到wp-admin中.正确的文件已加载到前端.Ajax请求在前端工作-但在后端工作.使用此代码对Ajax后端进行处理时,不会执行"if".

The correct file is loaded in wp-admin. The correct file is loaded front-end. Ajax-requests works front-end - but not back end. The "if" is not executed when doing Ajax back-end with this code.

在添加以下"else if"代码时,它可以在后端工作,但当然不能在前端工作.

When adding the following "else if" code it works back-end, but then not frond-end of course:

else if ( is_admin() ) {
   require_once('admin/functions_admin.php'); 
}

推荐答案

通过在第三个文件中收集所有AJAX函数(前端和后端)来解决此问题:

Sorted this out by gathering all AJAX-functions (both front- and back-end) in a third file:

// Is admin, but not doing ajaax
if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
    require_once('admin/functions_admin.php'); 
}
// Is doing AJAX 
else if ( is_admin() && ( defined( 'DOING_AJAX' ) || DOING_AJAX ) ) {
    require_once('functions_ajax.php'); 
}
// Front-end functions
else { 
require_once('public/functions_public.php'); 
}

这篇关于WordPress插件中的is_admin()和DOING_AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆