mrkoopie / wp_ajaxfilter
在WordPress中生成HTML表单和筛选存档数据
Requires
- league/flysystem: ^1.0
- mrkoopie/wp_wrapper: ^1.2
Requires (Dev)
- mockery/mockery: ^0.9.4
- phpspec/phpspec: ^2.4
README
此包旨在成为WordPress主题或插件的组成部分。通过安装此代码,您可以添加高级筛选器,而无需编写HTML、PHP、javascript和CSS代码的每一部分。该包仍在开发中,可能不
如何安装
运行以下命令
composer require mrkoopie/wp_ajaxfilter
在您的functions.php中添加以下代码
function ajaxfilter($filter_id) { if(!isset($GLOBALS['WP_ajaxfilter'][$filter_id])) $GLOBALS['WP_ajaxfilter'][$filter_id] = new MrKoopie/WP_ajaxfilter/generator($filter_id); return $GLOBALS['WP_ajaxfilter'][$filter_id]; } require_once('vendor/autoload.php');
如何在主题中使用代码
代码设计为在您的functions.php(或任何包含在functions.php中的文件)内部有一个控制器,并在模板文件中有一个简单的调用。
# functions.php /** * Here we configure the ajaxfilter with the form id your_filter_id. * This id should be unique and is used in the template to output * the filter and is used as a selector in jQuery. */ ajax_filter('your_filter_id') // Configure the input fields ->add_checkbox(__('Province'), 'taxonomy_province', 'optional_tech_name_province') // The jQuery script will make an ajax call, set the // template filter here. The filter will use // get_template_part('your_ajax_template'), so you // can use the same input method. ->set_ajax_template('your_ajax_template') // By running render, we activate the query filter // and create the ajax listener. ->render(); # template.php /** * This command outputs the HTML form. Keep in mind that you have to put * a <div id="your_filter_id"></div> somewere! */ ajax_filter('your_filter_id')->html();
add_checkbox($label, $taxonomy_id, $tech_name )
$label显示在标签中。$taxonomy_id是加载数据的分类ID。$tech_name此名称用于技术字段名称(在每次GET请求中显示)。
set_ajax_template($template_name)
$template_name是您主题中模板的名称。此模板用于显示筛选结果。
render()
使用此函数设置查询筛选器并注册ajax调用。
如何覆盖占位符
- 在您的主题中创建目录overrides/wp_ajaxfilter_stubs/。
- 在那里创建您要覆盖的所有占位符文件。
占位符设计为灵活,应允许您创建正确的样式。如果您觉得这不是这样,请提交一个改进的占位符文件的pull request。
待办事项
添加支持
- 比较方法
待办事项:add_dropdown($label, $taxonomy_id, $tech_name )
$label显示在标签中。$taxonomy_id是加载数据的分类ID。$tech_name此名称用于技术字段名称(在每次GET请求中显示)。
待办事项:add_radiobuttons($label, $taxonomy_id, $tech_name )
$label显示在标签中。$taxonomy_id是加载数据的分类ID。$tech_name此名称用于技术字段名称(在每次GET请求中显示)。
待办事项:add_text($label, $field = 's', $tech_name )
$label显示在标签中。通过$field,您定义筛选应用到的字段。将此设置为s(默认)以使用默认的WordPress搜索字段。
常见问题解答
为什么这不是一个插件?
插件需要在后端提供GUI,而这不是本包提供的。此包旨在用于主题或插件中,以使开发者的生活更轻松。
为什么您使用Mockery进行模拟?
Prophecy的开发者没有包含对像__call()这样的魔术函数的支持。尽管他们有道理,但在这种情况下,我们需要使用__call来模拟WordPress函数,而无需预先定义所有可能的WordPress函数。
致谢
Jasper Kums,Eenvoud Media B.V. - 为提供反馈。