[英]Querying a function in functions.php?
I have a jQuery code that is going to check when the user is near the bottom of the page. 我有一个jQuery代码,用于检查用户何时靠近页面底部。 That's not the problem though.
但这不是问题所在。 This jQuery is going to send a AJAX request, giving it some details on what to load when the user is near the bottom of the page.
这个jQuery将发送一个AJAX请求,给出一些关于当用户靠近页面底部时要加载的内容的细节。 The code looks a bit like this at the moment:
目前代码看起来有点像这样:
$("<div>").load('?ajax=y&offset=something', function() {
$(".empty-div").append($(this));
setTimeout(function(){ console.log('after', $(document).height()); }, 0);
setTimeout(function(){ console.log('after', $(window).height()); }, 0);
});
My main problem is that I don't know what to query or how to go about sending the information to the PHP function in functions.php. 我的主要问题是我不知道要查询什么或如何将信息发送到functions.php中的PHP函数。 For example, I have at the moment this as my PHP function (until it's working):
例如,我现在有这个作为我的PHP函数(直到它工作):
function get_posts_page() {
if(isset($_GET['offset'])) {
echo"Hello!";
}
}
I'm aware the wordpress has add_action
and all that but I have no idea what I would apply as an action to either function to make the PHP recieve the data the Javascript is sending. 我知道wordpress有
add_action
和所有这些,但我不知道我将什么作为一个动作应用于使PHP接收Javascript发送的数据。 Is there a URL where all functions are parsed or something? 是否存在解析所有函数或其他内容的URL? Thanks for any help in advance.
在此先感谢您的帮助。 So how do I get the data from the Javascript to the PHP function in functions.php, in my theme directory?
那么如何从我的主题目录中的functions.php中获取Javascript中的数据到PHP函数呢?
I just made a video to show you how to use the add_action
request in WordPress. 我刚刚制作了一个视频,向您展示如何在WordPress中使用
add_action
请求。 You can watch it here . 你可以在这里观看 。
Here's my javascript 这是我的javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
$('#branding img').click(function() {
$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'my_unique_action',
offset: 5
}, function(data) {
$('#content').prepend('<p>' + data + '</p>');
});
});
</script>
And the php that I used in functions.php
我在
functions.php
使用的php
// Make sure it runs when the user is logged in,
// and when they are not.
add_action('wp_ajax_my_unique_action', 'get_offset');
add_action('wp_ajax_nopriv_my_unique_action', 'get_offset');
function get_offset() {
if( isset($_POST['offset']) ) {
echo 'Your ajax request was successful. Here was your offset: <strong>' . $_POST['offset'] . '</strong>';
}
die;
}
Reference: http://codex.wordpress.org/AJAX_in_Plugins 参考: http : //codex.wordpress.org/AJAX_in_Plugins
You're trying to call a PHP function from Javascript, correct? 你试图从Javascript调用PHP函数,对吗?
You'll need some logic on some page which calls get_posts_page()
. 你需要一些调用
get_posts_page()
页面上的逻辑。 Either you can create a new page getPostsPage.php?offset=
or you can put some logic in functions.php
, something like 要么你可以创建一个新页面
getPostsPage.php?offset=
或者你可以在functions.php
一些逻辑,比如
if(isset($_GET['function']) { switch($_GET['function']) { case 'get_posts_page': get_posts_page(); } }
However, the former approach is recommended; 但是,建议采用前一种方法; you don't want someone to be able to modify the
function
parameter and access deleteAllPosts()
maliciously. 你不希望有人能够修改
function
参数并恶意访问deleteAllPosts()
。
Therefore: 因此:
// getPostsPage.php require PATH . 'functions.php'; get_posts_page(); //checks $_GET['offset']
And remember to fail gracefully (do not expose an error message) if 'offset' is not set, or whatever. 如果没有设置'offset',或者其他什么,请记住优雅地失败(不要暴露错误信息)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.