Skip to content

Instantly share code, notes, and snippets.

@vajrasar
Created June 27, 2015 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vajrasar/8a8aa97de1b39f18ef29 to your computer and use it in GitHub Desktop.
Save vajrasar/8a8aa97de1b39f18ef29 to your computer and use it in GitHub Desktop.
Working php session logic (changes made in functions.php file)
function vg_show_post_id() {
$p_id = $_REQUEST['post_id'];
$title = get_the_title( $p_id );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
if (!empty($_SESSION['cart']) && array_key_exists( $p_id, $_SESSION['cart'] ) ) {
$cnt = $_SESSION['cart'][$p_id];
$cnt++;
$_SESSION['cart'][$p_id] = $cnt;
} else {
$_SESSION['cart'][$p_id] = 1;
}
echo '<pre>';
print_r( $_SESSION['cart'] );
echo '</pre>';
die();
} else {
echo "Not in admin-ajax";
}
}
add_action( 'wp_ajax_nopriv_vg_show_post_id', 'vg_show_post_id' ); // for ajax
add_action( 'wp_ajax_vg_show_post_id', 'vg_show_post_id' ); // for ajax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment