1

Hi this my code for wordpress plugin

function my_action_callback() {
      check_ajax_referer( 'my-special-string', 'security' );

      $whatever = intval( $_POST['whatever'] );
      $whatever += 10;
      echo $whatever;
      die(); 
    }
    add_action( 'wp_ajax_my_action', 'my_action_callback' );
    add_shortcode('my_products','my_shortcode');

        function my_shortcode($atts)
        {
            extract(shortcode_atts(array(
                'id'=>''       
            ), $atts));
         ob_start();

         wp_enqueue_script( 'my-script', plugins_url( 'myplugin/js/example.js' ), array('jquery'), NULL, true);
         wp_localize_script( 'script-name', 'MyAjax', array(
             'ajaxurl' => admin_url( 'admin-ajax.php' ),
             'security' => wp_create_nonce( 'my-special-string' )
          ));
        }

example.js code

jQuery(document).ready(function($) {

  var data = {
    action: 'my_action',
    security : MyAjax.security,
    whatever: 1234
  };

  $.post(MyAjax.ajaxurl, data, function(response) {
    alert('Got this from the server: ' + response);
  });
});

It does not alert any values. why? my shortcode [my_products]

Boopathi Rajan
  • 1,212
  • 15
  • 38
  • 1
    open up the debug log in chrome and check the Network traffic, it will help you and us with more info – Aliendroid Jul 11 '14 at 06:53
  • Or use the web developer tools in Firefox and do the same thing ^. Incidentally, you can use the `ajaxError()` function or just use `$.ajax({});` instead and in there you can define your on-error method. – Yes Barry Jul 11 '14 at 06:56
  • possible duplicate of [How to Use AJAX in a WordPress Shortcode?](http://stackoverflow.com/questions/13498959/how-to-use-ajax-in-a-wordpress-shortcode) – brasofilo Jul 11 '14 at 07:39
  • @brasofilo tell me to what i am doing wrong in above code – Boopathi Rajan Jul 11 '14 at 07:44
  • `enqueue` and `localize` are not using the same [handle](http://codex.wordpress.org/Function_Reference/wp_enqueue_script). The browser inspector would have told you that `MyAjax not defined`. – brasofilo Jul 11 '14 at 15:37

1 Answers1

0

You need to localize the script first before enqueuing it, also you are currently localizing script-name whilst enqueueing my-script.

Try this:

function my_shortcode($atts)
        {
            extract(shortcode_atts(array(
                'id'=>''       
            ), $atts));
         ob_start();
         // Register the script first.
         wp_register_script( 'script-name', plugins_url( 'myplugin/js/example.js' ), array('jquery'), NULL, true);
         // Now localize it.
         wp_localize_script( 'script-name', 'MyAjax', array(
             'ajaxurl' => admin_url( 'admin-ajax.php' ),
             'security' => wp_create_nonce( 'my-special-string' )
          ));
          // Now enqueue the script
          wp_enqueue_script( 'script-name')
        }
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
  • i fixed that. but it alert 0 always. why? – Boopathi Rajan Jul 11 '14 at 07:24
  • I suggest you log the response to the console of either firebug or chrome developer tools with `console.log(response)` and see what it contains, also have a look at the network traffic and see what is being returned. – Rob Schmuecker Jul 11 '14 at 07:30
  • most likely this is the result of intval...change your php function to test, just echo something else instead of $whatever, my guess is 0 will be gone. – David Jul 11 '14 at 07:56