【发布时间】:2017-11-18 16:46:06
【问题描述】:
我正在制作一个插件,它接收输入到表单中的关键词,然后处理这个词并在网页中进行搜索,当收到结果时(通过 AJAX),将筛选视频。
在我的 script.php 中,我没有回显所有 html 行,而是使用“include”来包含一个带有 html 行的 php 文件(托管在外部域中),然后将它们打印在域中 使用插件的人。一切正常,但在想要显示调用 Wordpress 内部函数的类别列表时,插件失败了,显然是因为脚本试图从托管文件的域而不是从本地wordpress。我怎么能这样做?
我的 scrypt.js 托管在本地域中,即包含在用户将下载的插件文件中。如您所见,它调用了托管在外部域中的 api.php。
<script>
jQuery(function($){
var pluginUrl = '<?php echo plugin_dir_url( __FILE__ ); ?>' ;
$('[id^="form-busqueda"]').on('submit', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : 'http://example.com/server/api.php',
data : $(this).serialize(),
cache: false,
beforeSend: function(){
$('#results').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />');
}
}).done(function(data) {
$('#results').html(data);
});
});
});
</script>
好的,这个查询将通过 AJAX 传递到我在另一个域中托管的 api.php 文件,这个文件将以“include (” results.php “)”作为响应,results.php 文件也位于同一个域中我的 api.php 文件在哪里
api.php
<?php
//A series of validations are made, variables are generated (example $variable1, and finally the file "results.php" is called that calls all the variables so that finally the content is printed in the site of the client that is using the plugin.
include("results.php");
?>
结果.php
<div class="container">
<p><?php echo $variable1 ?></p>
<p><?php echo $variable2 ?></p>
<p><?php echo $variable3 ?></p>
<?php
$category=addslashes($_GET['cat']);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'selected' => $category,
'name' => 'cat_',
);
wp_dropdown_categories( $args );
?>
</div>
问题是当我从这个文件调用 Wordpress 函数时。但我不知道我还能用什么。我希望我对我的问题已经足够清楚了。提前致谢。
【问题讨论】:
标签: php jquery ajax wordpress function