Javascript 如何在WordPress插件中调用AJAX?
我想通过AJAX将MySQL表(自定义表)数据获取到我的管理面板。当用户单击按钮时,无需页面刷新即可获取数据。 我已经在PHP上创建了它,但是在WordPress中在哪里添加AJAX文件呢 这是我的密码。注意,当用户单击fetchdata按钮调用AJAX以获得结果时,我正在尝试制作插件。 我在插件中添加了JS文件 我从主插件文件调用了jsJavascript 如何在WordPress插件中调用AJAX?,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我想通过AJAX将MySQL表(自定义表)数据获取到我的管理面板。当用户单击按钮时,无需页面刷新即可获取数据。 我已经在PHP上创建了它,但是在WordPress中在哪里添加AJAX文件呢 这是我的密码。注意,当用户单击fetchdata按钮调用AJAX以获得结果时,我正在尝试制作插件。 我在插件中添加了JS文件 我从主插件文件调用了js function school_register_head() { $siteurl = get_option('siteurl'); $u
function school_register_head() {
$siteurl = get_option('siteurl');
$url2 = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/jquery-1.3.2.js';
$url3 = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/script.js';
echo "<script src=$url2 type=text/javascript></script>\n";
echo "<script src=$url3 type=text/javascript></script>\n";
}
add_action('admin_head', 'school_register_head');
display.php文件
<?php
global $wpdb;
$rows = $wpdb->get_results("SELECT postid from `wp_school_post`");
echo "<table class='wp-list-table widefat fixed'>";
echo "<tr><th>ID</th><tr>";
foreach ($rows as $row ){
echo "<tr>";
echo "<td>$row->postid</td>";
echo "</tr>";}
echo "</table>";
?>
如果“将AJAX文件放在何处”意味着如何将其包含到管理页面:最简单的方法是使用管理队列脚本()
,例如,如果在主题中实现它:
<?php
function load_ajax_script() {
wp_enqueue_script("ajax-script", get_template_directory() . "/scripts/ajax-script.js");
}
add_action("admin_enqueue_scripts", "load_ajax_script");
?>
请注意,调用.php文件时必须使用URL,而不是使用php的include()
时服务器上的绝对路径。根据脚本的放置位置,可以使用$(…)
或jQuery(…)
现在我们必须插入调用AJAX的按钮,当然还有插入返回内容的容器。因为你没有说它显示在哪里,我现在就把它放在管理区的顶部:
function display_ajax_button() {
?>
<input type="button" value="Fetch data" id="fetch-data" />
<div id="fetched-data-content"></div>
<?php
}
add_action("admin_init", "display_ajax_button");
这应该是最简单的方法。打开管理页面并尝试一下。让我知道它是否工作。不工作我想我把代码放在哪里弄错了:(这是在本地服务器上,不是实时站点。我不能在函数php中放置任何代码,因为我想构建插件,所以我希望所有代码都在插件文件夹中。我想我已经详细解释过了,不是吗?我在示例中使用的代码无法复制和粘贴,您必须根据您的设置进行一些小的更改。给我几分钟,我会的。)我给出了一个示例文件。好的,我刚刚编写了一个非常简单的插件,其中包含一个菜单页,带有一个按钮,可以获得一些帖子。将文件夹移动到you/plugins/目录中,激活它并导航到管理区域内的“AJAX示例”。有关更多说明,请参阅代码文件中的注释。你救了我的命:)衷心感谢thanx工作良好,事实上我是初学者,所以我再次提出问题,谢谢
<?php
function load_ajax_script() {
wp_enqueue_script("ajax-script", get_template_directory() . "/scripts/ajax-script.js");
}
add_action("admin_enqueue_scripts", "load_ajax_script");
?>
jQuery(document).ready(function() {
jQuery("#fetch-data").click(function() {
var script_url = "http://www.example.com/wp-content/themes/my-theme/display.php";
var response_container = jQuery("#responsecontainer");
jQuery.ajax({
type: "GET",
url:script_url,
success:function(response) {
response_container.html(response);
},
error:function() {
alert("There was an error while fetching the data.");
}
});
});
});
function display_ajax_button() {
?>
<input type="button" value="Fetch data" id="fetch-data" />
<div id="fetched-data-content"></div>
<?php
}
add_action("admin_init", "display_ajax_button");
<?php
global $wpdb;
$rows = $wpdb->get_results("SELECT postid from `wp_school_post`");
echo "<table class='wp-list-table widefat fixed'>";
echo "<tr><th>ID</th><tr>";
foreach ($rows as $row ){
echo "<tr>";
echo "<td>$row->postid</td>";
echo "</tr>";}
echo "</table>";
?>