php - How do I properly refresh data on Wordpress using AJAX -


i trying refresh widget on dashboard in wordpress.

my plugin written in php, trying add jquery/ajax refresh each widget when changes have been made.

the function in php download , store file, , display information widget.

the issue have this: files downloaded , stored. new information not show. stays same. believe might simple i've been trying work while no success.

here sample of code:

<?php  add_action( 'admin_footer', 'my_action_javascript' ); // write our js below here   function my_action_javascript() { ?> <script type="text/javascript" > jquery(document).ready(function($) {      var data = {         'action': 'refresh_function',     };               setinterval(function() {     $.get(ajaxurl, data, function (result) {      });              },20000);  }); </script> <?php }  add_action('wp_ajax_refresh_function', 'display_function'); 

php function:

function display_function() {   $conn_id = ftp_connect($ftp_server);  $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);  if (ftp_get($conn_id, $local_file, $server_file, ftp_binary)) {   ftp_close($conn_id);  $path = __dir__."/xml/file.xml";   $channel = simplexml_load_file($path);      echo "<div style='overflow:auto; height:400px;'>";       foreach ($channel->channel->item $item)       {                      echo "<div class='xpparticle'>";         echo "<h2>" . $item->title . "</h2>";                          echo "<p>". set_paragraph_length_display($item->description, 250) . "(...)" . "</p>";                global $wpdb;               $query = $wpdb->prepare('select id ' . $wpdb->posts . ' post_title = %s', $item->title);              $cid = $wpdb->get_var($query);                 echo "<hr />";         echo "</div>";     } echo "</div>";     } 

my understand ajax call function , work would. guess wrong because nothing new being displayed on screen once download new file using ajax.

thanks!

your ajax callback function empty. select element want put ajax output , set innerhtml code.

$.get(ajaxurl, data, function (result) {     $( '#my-dashboard-widget-content-element' ).html( result ); }); 

make sure have appropriate action set, in other answer:

add_action('wp_ajax_refresh_function', 'display_function' ); add_action('wp_ajax_nopriv_refresh_function', 'display_function' ); 

also, should use nonces verify authorized ajax request. in end of php function, dont forget die();

for comprehensive overview, check http://codex.wordpress.org/ajax_in_plugins


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -