Advertisement
Guest User

Wordpress plugin ajax return always 0

a guest
Oct 25th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2. /*
  3.     Plugin Name: TEST PLUGIN
  4.     Description: TEST
  5.     Author: Krzysztof Kubiak
  6.     Version: 1.0
  7. */
  8. add_action('admin_init', 'Test_01_settings_init' );
  9. function Test_01_settings_init(){
  10.     register_setting( 'Test_01_settings_filed', 'Test_01_options', 'Test_01_validate' );
  11. }
  12.  
  13. add_action('init', 'T01_init_method');
  14. function T01_init_method() {        
  15.     wp_enqueue_script('jquery');
  16.     wp_register_style( 'Test_01-style', plugins_url('css/style.css', __FILE__) );                
  17. }  
  18.  
  19. add_action('admin_menu', 'Test_01_menu_page');
  20. function Test_01_menu_page(){
  21.     add_menu_page( 'Test_01', 'Test_01', 'manage_options', 'T01_menu_page', 'T01_add_page' );
  22.     echo my_test();
  23. }
  24.  
  25. function my_test(){
  26.     echo 'test funkcji';
  27. }
  28.  
  29. function T01_add_page() {
  30.     echo 'TEST_01_plugin';
  31. }
  32.  
  33. function Test_01_validate($input) {
  34. }  
  35.  
  36. class Test_01_shortcode_selector extends WP_Widget {
  37.     function __construct() {
  38.         parent::__construct(
  39.             'T01_widget', // Base ID
  40.             __('T01_WIDGET', 'text_domain'), // Name
  41.             array( 'description' => __( 'TEST WIDGET', 'text_domain' ), ) // Args
  42.         );
  43.     }
  44.  
  45.     public function widget( $args, $instance ) {
  46.        
  47.         echo 'Test_01:<br>';
  48.    
  49.         function my_action_callback() {
  50.             global $wpdb; // this is how you get access to the database
  51.  
  52.             $whatever = 10;
  53.  
  54.                 return $whatever;
  55.                
  56.             exit();
  57.             // die(); // this is required to terminate immediately and return a proper response
  58.         }
  59.         add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
  60.         add_action( 'wp_ajax_my_action', 'my_action_callback' );
  61.        
  62.        
  63.         echo my_action_callback();
  64.         echo my_action_callback();
  65. ?>
  66.         <script type="text/javascript" >
  67.             var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
  68.             jQuery(document).ready(function($) {
  69.                 console.log('START');
  70.  
  71.                 var data = {
  72.                     'action': 'my_action',
  73.                     'whatever': 1234
  74.                 };
  75.  
  76.                 // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  77.                 $.post(ajaxurl, data, function(response) {
  78.                     console.log('Got this from the server: ' + response);
  79.                 });
  80.             });
  81.         </script> <?php
  82.     }
  83.    
  84.     public function form( $instance ) {
  85.         echo 'Widget';
  86.     }
  87.    
  88.     public function update( $new_instance, $old_instance ) {
  89.         $instance = array();
  90.     }
  91. }
  92.  
  93. add_action('widgets_init', 'register_Test_01_shortcode_selector');
  94. function register_Test_01_shortcode_selector() {
  95.     register_widget( 'Test_01_shortcode_selector' );
  96. }
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement