When a user visits a search result page, the user needs to click "clear search filters" which redirects the user to the search page. The session is cleared upon this action.
I'm looking for a way to destroy the session via ajax - so that the user can make use of the filter options directly on the search results page (without having to go back to the search page).
This the code I have now:
<?php session_start(); if($_GET['action']=='reset') {
session_destroy();
wp_redirect( 'http://website.com/search' ); exit;
} else {
if(!is_page(4097)) {
session_destroy(); }
} ?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
</head>
<body>
<?php if(!is_page(4097)) { ?>
<form name="search-form" method="post" action="<?php echo get_permalink(4097);?>">
<select name="one">
<?php echo $form_options_one; ?>
</select>
<select name="two">
<?php echo $form_options_two; ?>
</select>
<input type="submit" name="submit" value="Search">
</form>
<?php }?>
<div id="container">
search_and_results.php code is below:
<?php
session_start();
get_header();
if($_GET['action']=='reset'){
session_destroy();
wp_redirect( get_permalink(get_the_ID()) ); exit;
}?>
<div id="content">
<?php if(!isset($_POST['submit']) && (!isset($_SESSION['one'])) && (!isset($_SESSION['two']))) { ?>
<form name="search-form" method="post" action="<?php echo get_permalink(4097);?>">
<select name="one">
<?php echo $form_options_one; ?>
</select>
<select name="two">
<?php echo $form_options_two; ?>
</select>
<input type="submit" name="submit" value="Search">
</form>
<?php } else {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query($args); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $author_data= get_userdata($post->post_author); $author_meta= get_user_meta($post->post_author); ?>
<?php if(($author_data->roles[0]=='editor') || ($author_data->roles[0]=='contributor') || ($author_data->roles[0]=='subscriber') ) { ?>
// only get posts from editors, contributors, and subscribers
<!--post stuff here-->
<?php }?>
<?php endwhile; ?> <!-- end of the loop -->
<!-- pagination here -->
<?php if($the_query->max_num_pages==1) {
session_destroy();
}?>
<?php if ( $the_query->max_num_pages > 1 ) : ?>
<div class="main_navigation">
<?php
// usage with max_num_pages
next_posts_link( 'Next Page »', $the_query->max_num_pages );
previous_posts_link( '« Previous Page' );
?>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<div class="back-to-form clearfix">
<a href="<?php echo home_url('/') ?>?action=reset" class="clear_filter button">< Clear search filters</a>
</div>
<?php } ?>
</div><!-- end content id -->
</div><!--container-->
</body>
</html>
Is this possible given the structure above? Thanks so much
from what i understand, you need to change the way you structure your page to do this. first create an external page to make the ajax call and handle the session in that external page. for example, every time this external page loads your session sets to false like this
external-page.php
<?php
$_SESSION['searchfilters']=0;
//this will set to 0 any time this page is run by ajax or original url.
?>
second if you want load the search page without user having to leave the page, make another call by jquery or ajax that loads your search page into a designated div or some html tag.
your ajax function would be something like this
$.ajax({
type:"POST",
//data:'',
url:"http://www.yourdomain.com/external-page.php",//this will clear the session values to 0.
success: function(data){
//here you reload your search page into designated div with an id or class then user do not have to leave the page.
}, //end of success
error: function(){
//display any error message here
}// end of error
});
hope that helps you find your path.
A reminder, Do not use session_destroy(), if you are having users with accounts on your site, this will kill all the existing sessions for the current user and log them out.