wordpress FAQ plugin

This commit is contained in:
Egor Svitin
2019-02-19 10:21:31 +02:00
parent ae820d2169
commit a134080f14
8 changed files with 216 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
document.addEventListener("DOMContentLoaded", function(event) {
$('.start-search').on('click', function(){
$('#search-form').submit();
});
})

View File

@@ -75,6 +75,7 @@ require_once 'inc/walker-nav-menu.php';
<?php wp_footer(); ?>
<script src="<?= get_theme_file_uri('/assets/js/featuresScroll.js'); ?>"></script>
<script src="<?= get_theme_file_uri('/assets/js/faqSearch.js'); ?>"></script>
</body>
</html>

View File

@@ -65,6 +65,7 @@ add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('jquery-ui', '//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css', ['bootstrap'], null);
wp_enqueue_script('jquery-ui', '//code.jquery.com/ui/1.12.1/jquery-ui.min.js', ['custom-jquery'], null, true);
wp_enqueue_script('lawcarta-js', get_theme_file_uri('/assets/js/faqSearch.js'), ['custom-jquery'], null, true);
wp_enqueue_style('lawcarta-external-style', get_theme_file_uri('/assets/css/style.min.css'), ['jquery-ui']);
wp_enqueue_style('lawcarta-newcss', get_theme_file_uri('/assets/css/newcss.min.css'), ['lawcarta-external-style']);
@@ -122,3 +123,62 @@ add_filter('tiny_mce_before_init', function ($settings) {
$settings['valid_children']="+a[div|p|ul|ol|li|h1|h2|h3|h4|h5|h5|h6]";
return $settings;
});
function enableSearchQuery()
{
return !empty($_POST['search_text']);
}
function getFaqPosts($args) {
if (enableSearchQuery()) {
// WP magic
add_filter( 'posts_where', 'my_filter_post_where' );
}
$posts = get_posts($args);
if (enableSearchQuery()) {
remove_filter( 'posts_where', 'my_filter_post_where' );
}
return $posts;
}
function getFaaTaxonomyIds()
{
if (!enableSearchQuery())
{
return false;
}
$posts = getFaqPosts([
'post_type' => 'fac_category_answer',
'suppress_filters' => false,
'numberposts' => -1,
]);
if (count($posts) == 0) {
return [];
}
$tIds = [];
foreach ($posts as $post) {
$taxonomies = get_the_terms($post, 'fac_category_type');
if (count($taxonomies) > 0) {
foreach ($taxonomies as $taxonomy) {
if (!in_array($taxonomy->term_id, $tIds)) {
$tIds[] = $taxonomy->term_id;
}
}
}
}
return $tIds;
}
function my_filter_post_where( $where) {
global $wpdb;
if (!enableSearchQuery()) {
return $where;
}
$where .= ' AND (
' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $_POST['search_text'] ) . '%\' OR
' . $wpdb->posts . '.post_content LIKE \'%' . esc_sql( $_POST['search_text'] ) . '%\'
)';
return $where;
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* Template Name: Faq Page
*/
get_header();
$extraQueryParams = [];
$tIds = getFaaTaxonomyIds();
if ($tIds !== false) {
if (count($tIds) == 0) {
$terms = [];
} else {
$terms = get_terms( [
'taxonomy' => 'fac_category_type',
'hide_empty' => true,
'orderby' => 'term_order',
'include' => $tIds
]);
}
} else {
$terms = get_terms( [
'taxonomy' => 'fac_category_type',
'hide_empty' => true,
'orderby' => 'term_order',
]);
}
?>
<div class="main-page">
<div class="faq-search">
<div class="cont">
<form method="POST" id="search-form" action="/faq-page">
<input type="text" placeholder="What do you need help with?" name="search_text" value="<?= enableSearchQuery()?$_POST['search_text']:''?>">
<a href="#" class="start-search">
<span class="icon-search"></span>
</a>
</form>
</div>
</div>
<div class="breadcrumbs">
<div class="cont">
<ol class="breadcrumb">
<li><a href="#">Home</a></li>
<li class="active">FAQ</li>
</ol>
</div>
</div>
<div class="faq-list">
<div class="cont">
<?php foreach ($terms as $term):?>
<div class="front-accordion">
<div class="fa-head">
<a class="collapse-link" data-toggle="collapse" href="#fc-<?= $term->term_id ?>" aria-expanded="true"><span class="icon-caret"></span> <?= $term->name?></a>
</div>
<div class="fa-body collapse in" id="fc-<?= $term->term_id ?>">
<div class="fa-cont">
<?php
$posts = getFaqPosts(['post_type' => 'fac_category_answer',
'numberposts' => -1,
'suppress_filters' => false,
'tax_query' => [
[
'taxonomy' => 'fac_category_type',
'field' => 'id',
'terms' => $term->term_id,
'include_children' => false
]
]
]);
foreach ($posts as $post) {
echo '
<div class="faq-question">
<a href="'.get_permalink($post).'">'.$post->post_name.'</a>
</div>
';
}
?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php
get_footer();

View File

@@ -0,0 +1,46 @@
<?php
/**
* Template Name: Faq Answer
*/
get_header();
$post = $posts[0];
?>
<div class="main-page">
<div class="faq-search">
<div class="cont">
<form method="POST" id="search-form" action="/faq-page">
<input type="text" placeholder="What do you need help with?" name="search_text" value="<?= enableSearchQuery()?$_POST['search_text']:''?>">
<a href="#" class="start-search">
<span class="icon-search"></span>
</a>
</form>
</div>
</div>
<div class="breadcrumbs">
<div class="cont">
<ol class="breadcrumb">
<li><a href="/">Home</a></li>
<li class="active">FAQ</li>
</ol>
</div>
</div>
<div class="faq-answer">
<div class="cont">
<div class="answer-cont">
<h4><?= $post->post_name?></h4>
<div class="answer-text">
<?= $post->post_content?>
</div>
</div>
</div>
</div>
</div>
<?php
get_footer();

View File

@@ -0,0 +1,5 @@
document.addEventListener("DOMContentLoaded", function(event) {
$('.start-search').on('click', function(){
$('#search-form').submit();
});
})