javascript - Load .js file from wordpress plugin -


i'm trying load .js file wp plugin.

the code load jquery, jquery-ui , .js file this, , located inside "main" plugin file:

//load java , jquery function load_jquery() {      // use method we're not in wp-admin     if (!is_admin()) {          // deregister original version of jquery         wp_deregister_script('jquery');         wp_deregister_script('jquery-ui');         wp_deregister_script('lyox-script');          // discover correct protocol use         $protocol='http:';         if($_server['https']=='on') {             $protocol='https:';         }          // register google cdn version         wp_register_script('jquery', $protocol.'//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2');         wp_register_script('jquery-ui', $protocol.'//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', false, '1.10.3');         wp_register_script('lyox-script', plugins_url( '/includes/script.js' , __file__ ), array( 'jquery', 'jquery-ui' ) );          // add queue         wp_enqueue_script('jquery');         wp_enqueue_script('jquery-ui');         wp_enqueue_script('lyox-script');       }  }  add_action('template_redirect', 'load_jquery'); 

then inside .js file have following code, post() function added form button onclick="post();":

$(document).ready(function() {          function post() {          var name = $('#name').val();                $.post('process.php', {postname:name},                     function(data)                         {                         alert(data);                         $('#result').html(data);                                      });     } }); 

still nothing happens when try out on page. ideas?

you can using admin ajax : not explain functions . can google , learn .

step 1: localize values script used in javascript files,

get admin-ajax.php url

$author_nonce = wp_create_nonce( 'nk_author' );       wp_localize_script( 'nk_script', 'nk_object',array( 'nk_ajax_url' => admin_url( 'admin-ajax.php' ) , 'nk_plugin_url' => plugins_url() ,'nk_author' => $author_nonce) ); 

step 2 : in script can .

var data = {         action: 'nk_action', // function called in plugin         _ajax_nonce :   nk_object.nk_author, // nonce security         id : 'mydata'   //your data sent         };              //(admin ajax url , data , callback response )         jquery.post(nk_object.nk_ajax_url, data, function(response) {                 $('#nk_result').html(response);               }           });//end jquery.post 

step 3 : in plugin.php file this

<?php add_action('wp_ajax_nk_action', 'nk_action_callback');   function nk_action_callback() {     check_ajax_referer('nk_author');      if(isset($_post['id']))     {         $id=$_post['id'];             echo $id;     } die();//dont forget write die }?> 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -