我最近一直在玩AJAX到WordPress.我知道有很多插件可用,但我想自己制作.
在有关AJAXified WordPress的文章中,大多数人建议使用admin-ajax.php来处理AJAX请求.我的第一个想法是如何制作它只是创建自定义get_header()和get_footer()
// Boolean function ?ajax=true
function is_ajax () {
if($_REQUEST['ajax']) {
return true;
} else {
return false;
}
}
function ajax_get_header () {
if(is_ajax()) {
get_header('ajax');
/* Insert header-ajax.php which
includes only google analytics tracking code and some minor stuff */
return true;
} else {
get_header();
// Standard header
return true;
}
}
/* Function ajax_get_footer() pretty much the same */
Run Code Online (Sandbox Code Playgroud)
然后,页面模板看起来像
<?php ajax_get_header(); ?>
<!-- Content -->
<?php ajax_get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)
当然,制作ajax是标准方式.这种方法让我觉得简单干净.另一方面,许多人建议使用内置函数,通过创建一个钩子来捕获AJAX调用.