繁体   English   中英

PHP-从另一个域中的外部文件调用wordpress函数

[英]PHP - Call wordpress functions from an external file in another domain

我正在制作一个插件,该插件接收以表格形式输入的关键字,然后处理该单词并在网页中进行搜索,当接收到结果(通过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");
?>

results.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函数时。 但是我不知道我还能用什么。 我希望我对我的问题已经足够清楚。 提前致谢。

在大多数Web服务器(php.ini)中,设置为允许包含文件,因此出于安全原因,您不能使用include来包含来自远程地址的文件。

或者您可以使用它,在php.ini中必须将指令allow_url_include设置为On。

但是,如果您想读取远程文件的HTML内容,那么对您来说,使用file_get_contents函数可能会有所帮助,但是这将作为纯HTML标记代码返回,因此不会有任何服务器端代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM