字preSS插件调用通过AJAX数据库结果插件、数据库、结果、preSS

2023-09-10 21:33:40 作者:无缘`

我写了一个字preSS插件生成内容的网页,然后试图通过jQuery.load打电话Update.php刷新成绩。例如:

I have written a Wordpress plugin which generates content for a page, then tries to call Update.php via jQuery.load to refresh the results. For example:

插件生成此内容:

global $wpdb;
$sql = "SELECT * FROM table ORDER BY id;";
$results = $wpdb->get_results($sql);
<div id="content">Database Results Here Via Loop</div>

jQuery的通话Update.php:

jQuery calls Update.php:

<script type="text/javascript">
$(function() {
  var refresh = setInterval(function() {
    $("#content").load("Update.php");
  }, 5000);
});

Update.php包括:

Update.php contains:

<?php
function update_page() {
  global $wpdb;
  $sql = "SELECT * FROM table ORDER BY id;";
  $results = $wpdb->get_results($sql);
  echo "Parsed results go here";
}
update_page();
?>

5秒后,这将导致一个:主界面上的致命错误调用一个成员函数get_results()在一个非对象

如果我只是让Update.php返回类似兰特(),然后正常工作,并更新了随机数的股利。不过,如果我尝试以包括定义了Update.php文件中的所有字preSS类(即WPDB $)的头(例如:require_once(可湿性粉剂博客 - header.php文件);),则AJAX简单停止工作完全,而且也不会返回兰特(),但它的将会的prevent发生上述的致命错误。

If I simply make Update.php return something like rand(), then it works fine and updates the div with a random number. However, if I attempt to include the header that defines all the Wordpress classes (i.e. $wpdb) in the Update.php file (e.g. require_once("wp-blog-header.php");) then the AJAX simply stops working altogether, and won't even return a rand(), yet it will prevent the fatal error mentioned above from occurring.

我完全失去了。任何帮助或只是指着我在正确的方向将是极大的AP preciated。谢谢你在前进,善良SIRS。

I am completely lost. Any help or just pointing me in the right direction would be greatly appreciated. Thank you in advance, kind sirs.

推荐答案

要调用update.php文件中使用AJAX这样一句话preSS无法加载其他的文件,所以它没有找到WPDB $对象定义,所以你需要做的是 require_once的WP-config.php文件和WP-包括/ WP-db.php的文件

you are calling update.php file using ajax so wordpress is unable to load other its files so it is not finding $wpdb objects definition so you need to do is require_once the wp-config.php and wp-includes/wp-db.php files

和创建一个对象作为 $ = WPDB WPDB新(DB_USER,DB_PASSWORD,DB_NAME,DB_HOST);

在功能update_page();

before the function update_page();