php 如何将ajax添加到wordpress主题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10589018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add ajax to wordpress theme
提问by user1108310
I've got a problem that I'm stuck on for days now... I'm trying to use a simple ajaxPOST function to send data to a MySQL database (not WP database).
我有一个问题,我已经被困了好几天了......我正在尝试使用一个简单的 ajaxPOST 函数将数据发送到 MySQL 数据库(不是 WP 数据库)。
This code is located within "single-post.php" in the theme, because it must be checked before every post.
此代码位于主题中的“single-post.php”中,因为必须在每次发布之前检查它。
$.ajax({ url: 'library/functions/admin_checkuser.php',
data: {action: userID},
type: 'post',
success: function(output) {
alert(output);
}
});
I'm simply sending a variable to a "admin_checkuser.php" script which in turn calls another script that take actions on the database.
我只是将一个变量发送到“admin_checkuser.php”脚本,该脚本又调用另一个对数据库执行操作的脚本。
This is the code for "admin_checkuser":
这是“admin_checkuser”的代码:
$userid = $_POST['action'];
echo $userid;//for testing
$oMySQL = new MySQL();
$query = "Select * FROM videotable WHERE uid = '$userid'";
$oMySQL->ExecuteSQL($query);
$bb = $oMySQL->iRecords;
$aa = $oMySQL->aResult;
echo $bb;
if ($bb == 0){
$query = "INSERT INTO videotable VALUES ('','$userid','true')";
$oMySQL->ExecuteSQL($query);
echo 'true';
exit();
}else{
$sharing = mysql_result($aa,0,"share");
echo $sharing;
exit();
}
But I don't think the calls go through to the script. These scripts where tested outside of WordPress and did work, so it must be something in WordPress that blocking the ajax call. BTW, I tried placing the "admin_checkuser.php" in many diffrent folders but nothing worked.
但我认为这些调用不会传递到脚本中。这些脚本在 WordPress 之外进行了测试并且确实有效,因此它必须是 WordPress 中阻止 ajax 调用的东西。顺便说一句,我尝试将“admin_checkuser.php”放在许多不同的文件夹中,但没有任何效果。
Thanks in advance.
提前致谢。
采纳答案by Gohn67
You should check your url for your ajax call.
您应该检查您的 url 以获取 ajax 调用。
Maybe use the full url instead of the relative one.
也许使用完整的 url 而不是相对的。
It could be because of the location of your theme makes the url incorrect. I'm assuming your ajax code is in your theme folder.
这可能是因为您的主题位置使 url 不正确。我假设您的 ajax 代码在您的主题文件夹中。
There are some wordpress functions that get the theme directory.
有一些 wordpress 函数可以获取主题目录。
For example if you at this page http://yourwebpage/test/then the ajax call will go here http://yourwebpage/test/library/functions/admin_checkuser.php. This I assume would be the incorrect location.
例如,如果您在此页面,http://yourwebpage/test/则 ajax 调用将转到此处http://yourwebpage/test/library/functions/admin_checkuser.php。我认为这将是不正确的位置。
This is why you need to add the absolute url to your script. And if it is in your theme, you can use this method get_template_directory_uri()to get the template directory.
这就是为什么您需要将绝对 url 添加到脚本中的原因。并且如果它在你的主题中,你可以使用这个方法get_template_directory_uri()来获取模板目录。
See here: http://codex.wordpress.org/Function_Reference/get_template_directory_uri
见这里:http: //codex.wordpress.org/Function_Reference/get_template_directory_uri
回答by Stoosh
You're much better off using the inbuilt Wordpress AJAX request.
您最好使用内置的 Wordpress AJAX 请求。
So in your themes functions.phpfile, add your function to be called, for example:
因此,在您的主题functions.php文件中,添加要调用的函数,例如:
function checkUser() {
$userid = $_POST['user']; //validation also :)
$oMySQL = new MySQL();
$query = "Select * FROM videotable WHERE uid = '$userid'";
$oMySQL->ExecuteSQL($query);
$bb = $oMySQL->iRecords;
$aa = $oMySQL->aResult;
echo $bb;
if ($bb == 0){
$query = "INSERT INTO videotable VALUES ('','$userid','true')";
$oMySQL->ExecuteSQL($query);
echo 'true';
exit();
} else {
$sharing = mysql_result($aa,0,"share");
echo $sharing;
exit();
}
}
After that, you add your hook with connects to the inbuilt AJAX System
之后,您添加带有连接到内置 AJAX 系统的钩子
add_action('wp_ajax_check_user', 'checkUser');
add_action('wp_ajax_nopriv_check_user', 'checkUser');
wp_ajax_nopriv_%sallows it to be called from the front end.
wp_ajax_nopriv_%s允许它从前端调用。
And then, in your javascript file, you just fire off your ajax request.
然后,在您的 javascript 文件中,您只需触发 ajax 请求。
$.post(ajaxurl, { action: 'check_user', user: userId }, function(output) {
alert(output);
});
If ajaxurlis undefined, you will need to create it in your template file, something like this should work, but there are other ways.
如果ajaxurl未定义,则需要在模板文件中创建它,这样的方法应该可以工作,但还有其他方法。
add_action('wp_head','ajaxurl');
function ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}
Backend of wordpress does the rest.
剩下的由 wordpress 后端完成。
It takes the action passed in the AJAX request, looks for the corresponding `wp_ajax(_nopriv)_%shook and then calls the function that is assigned to the hook.
它接受在 AJAX 请求中传递的动作,寻找相应的`wp_ajax(_nopriv)_%s钩子,然后调用分配给钩子的函数。
It will also pass in either the $_POSTor $_GETdepending on the type of AJAX request.
它还将根据 AJAX 请求的类型传入$_POST或$_GET。
You can read a little more about using AJAX inside of Wordpress.
您可以阅读更多关于在 Wordpress 中使用AJAX 的信息。

