<script type="text/javascript"> // jQuery(document).ready( function($) { $.ajax({ url: "http://wordpress/", // success: function( data ) { // document.getElementById('qua').innerHTML="<?php echo $result['total_users']; ?>"; // } success : function(){ setInterval(function(){ document.getElementById('qua').innerHTML="<?php echo $result['total_users']; ?>"; // alert(); }, 10000); } }); // }); </script> 

I need to update the number of posts without reloading the page. I decided to start with a simple --- number of users. Alert works, but the number of users does not change until I reload the page

  • how do you execute this line document.getElementById('qua').innerHTML="<?php echo $result['total_users']; ?>"; ? in the browser pkhp-code? - alenkins

1 answer 1

Good afternoon, your code is not quite clear what.

I can explain on the fingers how you can easily implement ajax in wordpress

Create a page, give it a slug (for example ajax), create a template for this page (page-ajax.php)

 //Тут ставим действие или событие по которому мы будем отправлять запрос на нашу страницу //не обязательно использовать таймер setTimeout(function () { //Раз мы работает с JQUERY, то используем JQUERY //Первым параметром передаем адрес нашей страницы, а вторым параметром callback функцию которая примет то, что мы напишем в шаблоне page-ajax $.post('/ajax', function(data){ alert(data); }); }, 1000); 
 <?php global $wpdb; echo $wpdb->get_var("select count(*) from wp_users"); //У тебя может отличатся название таблицы, если оно не wp_users, замени. 
  • Thank you so much) - Oleh
  • Solved the question, and zaminusovali. Strange) I hope I helped you, please contact - lazyproger
  • Maybe by chance, sorry) - Oleh
  • Nothing wrong. Thank. He himself recently dealt with ajax in wordpress and realized that it is better to send all requests to 1 page, and already inside to process. It is more convenient than 100500 hooks in functions.php - lazyproger