Advertisement
kkarpieszuk

Wordpress Ajax testing plugin

Mar 28th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Aajax test
  5.  */
  6.  
  7. add_action( 'wp_footer', 'my_action_javascript' );
  8.  
  9. add_action( 'admin_footer', 'my_action_javascript' );
  10.  
  11. function my_action_javascript() {
  12. ?>
  13. <h3 id="ajax_answer" style="clear:both; text-align: center; background: white;" ></h3>
  14. <script type="text/javascript" >
  15. jQuery(document).ready(function($) {
  16.  
  17.     var data = {
  18.         action: 'my_action',
  19.         whatever: 1234
  20.     };
  21.    
  22.     var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ) ?>';
  23.  
  24.     $.post(ajaxurl, data, function(response) {
  25.     $('#ajax_answer').html('Got this from the server with Ajax: ' + response);
  26.     });
  27. });
  28. </script>
  29. <?php
  30. }
  31.  
  32.  
  33.  
  34. add_action( 'wp_ajax_my_action', 'my_action_callback' );
  35.  
  36. add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
  37.  
  38. function my_action_callback() {
  39.     global $sitepress;
  40.    
  41.     $lang = $sitepress->get_current_language();
  42.    
  43.     echo "Current language is $lang";
  44.  
  45.     die(); // this is required to return a proper result
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement