Im正在创建具有以下结构的插件:
plugins (folder)
- myplugin (folder)
- myplugin_index.php (adminpanel file)
- myplugin_ajax.php (loading data when user enters a form on the "start page")
themes (folder)
- mytheme (folder)
- template_my_own.php (this file have the form which calls the myplugin_ajax.php)
从管理面板的角度来看,该插件工作正常。我已经通过管理面板的插件添加了数据。
但是当我试图通过文件中的表单访问数据时template_my_own.php
我得到以下错误:
PHP Fatal error: Call to undefined function get_option()
in /var/www/example.com/wwwroot/wp-content/plugins/myplugin/myplugin_ajax.php
on line 14, referer: http://example.com/
我尝试了来自
both 这
site.
但它们都不起作用。我的代码如下所示:
<?php
/*
Plugin Name: Myplugin
Plugin URI: http://example.com
*/
?>
<?php
$square = $_POST[\'square\'];
//Get arrays and remove empty array values
$arrayFrom = array_filter(get_option(\'from\'), \'strlen\');
$arrayTo = array_filter(get_option(\'to\'), \'strlen\');
$arrayPrice = array_filter(get_option(\'price\'), \'strlen\');
//Sort arrays
asort($arrayFrom);
asort($arrayTo);
asort($arrayPrice);
//Go through values until we find the one
foreach($arrayFrom as $index => $from)
{
if($from < $square && $arrayto[$index] > $square)
{
echo $from;
echo \'<br>TESTTESTTEST\';
echo $to;
}
}
?>
我应该如何解决这个问题?我做错了什么?
SO网友:fischi
您应该将函数挂钩到WordPress AJAX Api。
function your_ajax_function() {
// your Script here
}
add_action( \'wp_ajax_your_ajax_function\', \'your_ajax_function\' );
add_action( \'wp_ajax_nopriv_your_ajax_function\', \'your_ajax_function\' ); // Skip this line if you want the AJAX just for logged in users
现在,您可以通过Javascript轻松调用函数:
var data = {
action: \'your_ajax_function\',
your_data: \'datatosubmit\'
};
$.post(ajaxurl, data, function(response) {
// what to do with your response
});
只需确保定义了ajaxurl即可。通常是这样
http://www.yoursite.com/wp-admin/admin-ajax.php
, 但这取决于你的WordPress设置。
如果使用这种方法,那么在AJAX调用中就可以使用所有WordPress函数。