admin-ajax.php与Ajax请求的自定义页面模板
-
-
顺便说一句:如果您想在您的wordpress网站上制作一个api,并且想要一个与api请求网址一样有意义的网址.然后,您可以进行一些mod重写,以便将http://site.com/api/映射到http://site.com/wp-admin/admin-ajax.php.BTW: if you want to make a api on your wordpress site and want a url that make sense as api request url. Then you can do some mod rewrite so `http://site.com/api/` maps to `http://site.com/wp-admin/admin-ajax.php`
- 0
- 2012-07-22
- Sisir
-
2 个回答
- 投票数
-
- 2012-07-22
首先,第一种方法的明显缺点是它依赖于您的特定页面,模板和永久链接结构才能正常工作.使用
admin-ajax.php
可以在任何上下文,主题或插件中正确使用WordPress最佳实践.第一种方法不太明显的缺点是,与整个WordPress支持的AJAX调用相比,它使用的内存更多,因为整个WordPress环境均已加载,因为将输出前端或管理页面.
在
admin-ajax.php
中添加NONCE可以提供简单的内置安全性.First, the obvious drawback to the first method is that it depends on your specific page, template, and permalink structure to all work correctly. Using
admin-ajax.php
will work correctly in any context, theme or plugin, where proper WordPress best practices are followed.The less obvious drawback to the first method is that it uses more memory than doing WordPress-enabled AJAX calls, since the whole WordPress environment is loaded, as it's presumed that a front-end or admin page will be output.
The addition of NONCEs with
admin-ajax.php
provides easy, built-in security.-
公平地说,整个WordPress环境+ admin区域也都为`admin-ajax.php`加载.唯一被跳过的是模板加载器.当然," admin-ajax.php"仍然是正确的方法.:)To be fair, the entire WordPress environment + the admin area is loaded for `admin-ajax.php` as well. The only thing that gets skipped is the template loader. `admin-ajax.php` is still the correct way, of course. :)
- 1
- 2012-07-22
- chrisguitarguy
-
- 2013-12-04
admin-ajax.php
并非总是正确的方法.例如,如果您要获取帖子,则实际上最好使用类似template_redirect()
之类的东西来加载返回JSON(或您需要返回的内容)的自定义模板.为什么?正在缓存.使用
admin-ajax.php
时,基本上是在消除某些缓存系统保存服务器响应输出的机会(通常不应使用admin URL,尤其是admin-ajax.php).已缓存).另一方面,使用template_redirect()
可以维护单独的URL,许多缓存插件和HTTP加速器可能会使用这些URL来保留数据.如果将某些后端缓存系统配置为避免在admin-ajax.php
时进行缓存,则即使某些后端缓存系统也可能不参与admin-ajax.php
.当然,如果您没有得到类似发布的静态信息,那么缓存实际上是一件非常糟糕的事情……在这种情况下,
admin-ajax.php
是一个更好的选择.admin-ajax.php
isn't always the right way to go. If you're looking to fetch a post, for example, you may actually better off using something liketemplate_redirect()
to load a custom template that returns JSON (or whatever you need returned).Why? Caching. When you use
admin-ajax.php
you're basically eliminating the opportunity for some cache systems to save the output of the server response (generally admin URLs, and specifically admin-ajax.php, should not be cached). Usingtemplate_redirect()
on the other hand allows for maintaining separate URLs that many cache plugins and HTTP accelerators would be likely to use to keep the data. Even some back-end caching systems may not involve themselves inadmin-ajax.php
if they're configured to avoid caching whenis_admin()
.Of course, if you're not getting something fairly static like a post, caching could actually be a really bad thing... in which case
admin-ajax.php
is a far better choice.-
您可以扩展一下吗?我认为没有理由不能使用`admin-ajax.php`来缓存AJAX调用.Can you expand upon this? I see no reason you can't cache an AJAX call using `admin-ajax.php`.
- 0
- 2013-12-04
- s_ha_dum
-
当然.缓存在admin-ajax.php调用中涉及的数据库查询,对象等可能是完全可能的,但实际上我不太确定其细节.但是,如果您使用的是基于URL的缓存,则不会涉及admin-ajax.php,因为您的所有AJAX请求都是针对传递了不同参数的那个URL. 但是,如果使用`template_redirect()`路由,则对不同帖子的AJAX调用实际上将是对不同URL的请求,从而使基于URL的缓存系统发挥作用.Sure. It may be perfectly possible to cache the DB query, objects, etc involved in an `admin-ajax.php` call, I'm actually not too sure about the details of that. But if you're using something that does URL-based caching, `admin-ajax.php` isn't going to be involved — since all your AJAX requests are to that one URL with just different parameters passed in. However, if you go the `template_redirect()` route, your AJAX calls for different posts will actually be requests to different URLs, allowing a URL-based caching system to work its magic.
- 0
- 2013-12-04
- Drywall
-
因此,您所谈论的是严格基于URL的缓存,但是您提到了钩子.如果OP可以将对象缓存写入回调中,则有许多钩子应该允许将对admin-adjax.php的请求进行缓存.我建议您编辑答案以澄清/略述一下.So what you are talking about is strictly URL-based caching, but you mentioned hooks. There are a number of hooks that should allow caching of requests to `admin-adjax.php`, plus, if the OP can write object caching into the callbacks. I would suggest you edit the answer to clarify/caveat it a bit.
- 0
- 2013-12-04
- s_ha_dum
-
谢谢.我进行了修正,以消除对钩子的引用并进行澄清.基于URL的缓存是最明确的情况,但是正如我的修正声明所提到的,如果在admin区域内有适当的缓存系统,那么这是有可能的(尽管我不能明确地说任何插件都可以/不可以)这样做),那么您仍然会错过.更好?Thanks. I have amended to eliminate the reference to hooks and to clarify. URL-based caching is the most clear-cut situation, but as my amended statement mentions, if whatever caching system is in place backs off within the admin area (which is a possibility though I can't definititely say any plugins do/don't do that) then you're still missing out. Better?
- 0
- 2013-12-04
- Drywall
是否有理由将admin-ajax.php用于ajax请求而不是自定义页面模板?
直到最近我才对admin-ajax.php有所了解,所以我一直在做的事情是创建一个这样的自定义页面模板:
而ajax调用将是URL http://mysite.com/api/使用我的API页面模板发布了一个空白页面.这似乎使我可以访问所有WordPress功能并吐出数据.
但是,最近我阅读了admin-ajax.php并了解了连接到WordPress数据库的另一种方法是调用URL http://mysite.com/wp-admin/admin-ajax.php 并具有以下功能:
以第一方式连接是否错误? admin-ajax.php是否提供额外的安全性?感谢您的输入!