php - Wordpress - save filter settings into cookie -
i have custom post type in wordpress, custom filters.
i able save user's filter cookie, when comes posts lists, last filter set automatically.
i have code saving cookie:
function set_newuser_cookie() { if ( is_admin() && $_get['post_type'] == 'tickets') { setcookie('bs_tickets_filter', $_server['query_string'], time()+3600*24*100, cookiepath, cookie_domain, false); } } add_action( 'init', 'set_newuser_cookie');
this code saves whole query string cookie, like:
orderby=status&order=asc&s&post_status=all&post_type=tickets&action=-1&m=0&status=0&type=0&priority=0&state=2135&author&paged=1&mode=list&action2=-1
how can set filter again, when user comes posts listing?
i think solved this:
function bs_set_filter_cookie() { if ( is_admin() && $_get['post_type'] == 'tickets' ) { // if theres filter request, update cookie if (isset($_get['status'])) { $query = http_build_query($_get); setcookie('bs_tickets_filter', $query, time()+3600*24*100, cookiepath, cookie_domain, false); } } // if there's no filter request, add variable $_get if (!isset($_get['status'])) { parse_str($_cookie["bs_tickets_filter"], $output); foreach ($output $key => $val) { $_get[$key] = $val; } } } add_action( 'init', 'bs_set_filter_cookie');
Comments
Post a Comment