Guest User

Untitled

a guest
Sep 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. //JavaScript
  2.  
  3. $(document).on('click', '.add-favorite', function() {
  4. var imgId = $(this).data('id');
  5. saveFavorite(imgId);
  6. });
  7.  
  8. function saveFavorite(imgId) {
  9. var data = {
  10. 'action': 'save_favorite',
  11. 'id' : imgId
  12. };
  13. jQuery.post(my_ajax_object.ajax_url, data, function(response) {
  14. alert(response);
  15. });
  16. }
  17.  
  18. // PHP
  19.  
  20. function save_favorite() {
  21. ob_start();
  22. debug_to_console('saving favorite');
  23.  
  24. if ($_POST['id']) {
  25. $post_id = $_POST['id'];
  26. save_favorite_in_session_variable($post_id);
  27. }
  28. wp_die();
  29. }
  30. add_action('wp_ajax_save_favorite', 'save_favorite');
  31. add_action('wp_ajax_nopriv_save_favorite', 'save_favorite');
  32.  
  33. function save_favorite_in_session_variable($post_id) {
  34. array_push($_SESSION['favorites'], $post_id);
  35. }
Add Comment
Please, Sign In to add comment