✨feature: Homepage filter template
This commit is contained in:
11
README.md
11
README.md
@@ -1,7 +1,7 @@
|
||||
# Resource Filter Plugin
|
||||
# Content Filter Plugin
|
||||
|
||||
## Description
|
||||
The Resource Filter Plugin adds filtering capabilities for the 'resource' post type by 'resource_type' and 'resource_subject'. This plugin provides a shortcode to display a filter form and dynamically updates the resource results based on the selected filters.
|
||||
The Content Filter Plugin adds filtering capabilities for any registered post type by any taxonomies attched to it. This plugin provides a shortcode to display a filter form and dynamically updates the resource results based on the selected filters.
|
||||
|
||||
## Installation
|
||||
1. Download the plugin files and place them in the `wp-content/plugins/resource-filter` directory.
|
||||
@@ -15,8 +15,13 @@ The Resource Filter Plugin adds filtering capabilities for the 'resource' post t
|
||||
To override the default form and results templates, create a `resource-filter` directory in your theme and copy the `filter-form.php` and `resource-results.php` files from the plugin `templates` directory to your theme directory. The plugin will use the template files in your theme directory instead of the default template files.
|
||||
|
||||
## Changelog
|
||||
### 1.1.0 - 2025-02-04
|
||||
### 1.2.0 - 2025-02-05
|
||||
- Name change to reflect ability to filter more than 'resources'
|
||||
- Add secondary search template
|
||||
|
||||
### 1.1.0 - 2025-02-05
|
||||
- Fully templated for use in any theme
|
||||
- Added result sorting
|
||||
|
||||
### 1.0.0 - 2025-02-04
|
||||
- Initial release
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Resource Filter
|
||||
* Description: Adds filtering for the 'resource' post type by 'resource_type' and 'resource_subject'.
|
||||
* Version: 1.1.0
|
||||
* Plugin Name: Content Filter
|
||||
* Description: Adds filtering for the content typed by various taxonomies.
|
||||
* Version: 1.2.0
|
||||
* Author: Keith Solomon
|
||||
*/
|
||||
|
||||
@@ -60,17 +60,92 @@ class ResourceFilterPlugin {
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function renderFilterForm() {
|
||||
public function renderFilterForm($atts) {
|
||||
$atts = shortcode_atts([
|
||||
'type' => 'default' // Accepts 'default' or 'homepage'
|
||||
], $atts, 'resource_filter');
|
||||
|
||||
ob_start();
|
||||
|
||||
$query = new WP_Query([
|
||||
'post_type' => 'resource',
|
||||
'posts_per_page' => -1,
|
||||
]);
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc';
|
||||
|
||||
$resTotal = $query->found_posts; // Get the count of published resources
|
||||
$query_args = [
|
||||
'post_type' => 'resource',
|
||||
'posts_per_page' => -1,
|
||||
'tax_query' => [],
|
||||
's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
];
|
||||
|
||||
$resForm = rfGetTemplate('filter-form.php');
|
||||
// Sorting logic
|
||||
switch ($sort_order) {
|
||||
case 'date_asc':
|
||||
$query_args['orderby'] = 'date';
|
||||
$query_args['order'] = 'ASC';
|
||||
break;
|
||||
|
||||
case 'date_desc':
|
||||
$query_args['orderby'] = 'date';
|
||||
$query_args['order'] = 'DESC';
|
||||
break;
|
||||
|
||||
case 'title_asc':
|
||||
$query_args['orderby'] = 'title';
|
||||
$query_args['order'] = 'ASC';
|
||||
break;
|
||||
|
||||
case 'title_desc':
|
||||
$query_args['orderby'] = 'title';
|
||||
$query_args['order'] = 'DESC';
|
||||
break;
|
||||
|
||||
default:
|
||||
$query_args['orderby'] = 'date';
|
||||
$query_args['order'] = 'DESC';
|
||||
}
|
||||
|
||||
$tax_query = [];
|
||||
|
||||
if (!empty($_POST['resource_type'])) {
|
||||
$resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']);
|
||||
|
||||
$query_args['tax_query'][] = [
|
||||
'taxonomy' => 'resource_type',
|
||||
'field' => 'slug',
|
||||
'terms' => $resType,
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($_POST['resource_subject'])) {
|
||||
$query_args['tax_query'][] = [
|
||||
'taxonomy' => 'resource_subject',
|
||||
'field' => 'slug',
|
||||
'terms' => array_map('sanitize_text_field', $_POST['resource_subject']),
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = [
|
||||
'relation' => 'AND', // Both filters must match
|
||||
...$tax_query
|
||||
];
|
||||
}
|
||||
|
||||
$query = new WP_Query($query_args);
|
||||
} else {
|
||||
$query = new WP_Query([
|
||||
'post_type' => 'resource',
|
||||
'posts_per_page' => -1,
|
||||
]);
|
||||
}
|
||||
|
||||
$resTotal = $query->found_posts; // Default total resource count
|
||||
|
||||
// Determine which form template to load
|
||||
$attTmpl = ($atts['type'] === 'homepage') ? 'filter-homepage.php' : 'filter-form.php';
|
||||
$resForm = rfGetTemplate($attTmpl);
|
||||
$summary = rfGetTemplate('filter-summary.php');
|
||||
|
||||
if ($resForm) {
|
||||
@@ -79,18 +154,26 @@ class ResourceFilterPlugin {
|
||||
echo '<p>Error: Form template not found.</p>';
|
||||
}
|
||||
|
||||
if ($atts['type'] === 'default') {
|
||||
if ($summary) {
|
||||
include_once $summary;
|
||||
}else {
|
||||
} else {
|
||||
echo '<p>Error: Summary template not found.</p>';
|
||||
}
|
||||
?>
|
||||
|
||||
<div id="resource-results">
|
||||
<?php $this->loadResources(); ?>
|
||||
<?php
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$this->filterResources(); // Directly render filtered results
|
||||
} else {
|
||||
$this->loadResources(); // Load all resources initially
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
@@ -132,7 +215,11 @@ class ResourceFilterPlugin {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function filterResources() {
|
||||
check_ajax_referer('resource_filter_nonce', 'nonce');
|
||||
$is_ajax = defined('DOING_AJAX') && DOING_AJAX;
|
||||
|
||||
if ($is_ajax) {
|
||||
check_ajax_referer('resource_filter_nonce', 'nonce');
|
||||
}
|
||||
|
||||
$sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc';
|
||||
|
||||
@@ -173,10 +260,12 @@ class ResourceFilterPlugin {
|
||||
$tax_query = [];
|
||||
|
||||
if (!empty($_POST['resource_type'])) {
|
||||
$resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']);
|
||||
|
||||
$query_args['tax_query'][] = [
|
||||
'taxonomy' => 'resource_type',
|
||||
'field' => 'slug',
|
||||
'terms' => array_map('sanitize_text_field', $_POST['resource_type']),
|
||||
'terms' => $resType,
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
@@ -211,19 +300,23 @@ class ResourceFilterPlugin {
|
||||
echo '<p>Error: Results template not found.</p>';
|
||||
}
|
||||
|
||||
// Prepare response JSON
|
||||
$response = [
|
||||
'count' => $query->found_posts,
|
||||
'filters' => [
|
||||
'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '',
|
||||
'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : ''
|
||||
],
|
||||
'html' => ob_get_clean()
|
||||
];
|
||||
if ($is_ajax) {
|
||||
// Prepare response JSON
|
||||
$response = [
|
||||
'count' => $query->found_posts,
|
||||
'filters' => [
|
||||
'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '',
|
||||
'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : ''
|
||||
],
|
||||
'html' => ob_get_clean()
|
||||
];
|
||||
|
||||
echo json_encode($response);
|
||||
wp_die();
|
||||
echo json_encode($response);
|
||||
wp_die();
|
||||
} else {
|
||||
echo ob_get_clean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
templates/filter-homepage.php
Normal file
20
templates/filter-homepage.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php if (!defined('ABSPATH')) { exit; } ?>
|
||||
|
||||
<form id="homepage-filter" action="<?php echo site_url('/browse-resources/'); ?>" method="GET">
|
||||
<?php
|
||||
$resource_types = get_terms(['taxonomy' => 'resource_type']);
|
||||
|
||||
if (!empty($resource_types)) :
|
||||
?>
|
||||
<select name="resource_type">
|
||||
<option value="">All Types</option>
|
||||
<?php foreach ($resource_types as $type) : ?>
|
||||
<option value="<?php echo esc_attr($type->slug); ?>"><?php echo esc_html($type->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php endif; ?>
|
||||
|
||||
<input type="text" name="search" placeholder="Search resources...">
|
||||
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
Reference in New Issue
Block a user