在wordpress插件文件中包含wp-config.php文件的问题


Issue to include wp-config.php file in wordpress plugin file

我正在开发一个插件,在插件中我创建了一个 AJAX(在插件索引文件中),当用户在搜索栏中写东西时,相关帖子标题会显示在下拉列表中,为了获得该结果,我为此创建了一个其他文件,例如插件根目录中的"foo_get_posts.php"。这是我的代码:

function foo_search_form(){
?>
    <div class="foo_search_field">
        <form role="search" method="get" id="searchform" class="clearfix" action="<?php echo home_url( '/' ); ?>" autocomplete="off">
            <input type="text" onfocus="if (this.value == '<?php _e("Search Posts...", "foo") ?>') {this.value = '';}" onblur="if (this.value == '')  {this.value = '<?php _e("Search Posts...", "foo") ?>';}" value="<?php _e("Search Posts...", "foo") ?>" name="s" id="s" />
            <ul id="foo_search_dropdown"></ul>
            <input type="hidden" name="post_type" value="foo_base" />
        </form>
    </div>
<?php
}
add_action('wp_head', 'foo_search_drop');
function foo_search_drop(){
?>
<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#s').keyup(function() {
            jQuery('#foo_search_dropdown').slideDown("slow");
            // Get data
            var foo_search_term = jQuery(this).val();
            // Sending data through AJAX to process
            jQuery.post('<?php echo plugin_dir_url(__FILE__) ?>foo_get_posts.php', {foo_search_key: foo_search_term}, function(data) {
                jQuery('#foo_search_dropdown').html(data);
            });
        });
    });
</script>
<?php
}

foo_get_posts.php文件代码:

<?php
require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php');
global $wpdb;
$foo_search_key = $_POST['foo_search_key'];
$tbl_posts = $wpdb->prefix."posts";
$tbl_relation = $wpdb->prefix."term_relationships";
$tbl_taxonomy = $wpdb->prefix."term_taxonomy";
$tbl_terms = $wpdb->prefix."terms";
if(!empty($foo_search_key)){
    $foo_search_sql = $wpdb->get_results(" // My SQL Query goes here");
    if($foo_search_sql){
        foreach ($foo_search_sql as $search_result) {
        ?>
            <li>
                <a href="<?php echo site_url()."/".foo_PLUGIN_SLUG."/".$search_result->post_name ?>">
                    <?php echo $search_result->post_title; ?>
                </a>
            </li>
            <?php
        }
    } else {
        ?>
            <span>Search result not found......</span>
<?php
    }
}
?>

首先,我不在我的文件中调用require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php');,所以我没有得到我的结果,所以我为此问了一个问题,现在我的工作进展顺利。

问题

现在我的插件完成了,我把它发送到wordpress,他们拒绝了它,他们说:

## Calling core loading files directly
You're calling this in foo_get_posts.php
require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php');
Including wp-config.php, wp-blog-header.php, wp-load.php, or pretty much any other WordPress core file that you have to call directly via an include is not a good idea and we cannot approve a plugin that does so unless it has a very good reason to load the file(s). It is prone to failure since not all WordPress installs have the exact same file structure.
Usually plugins will include wp-config.php or wp-load.php in order to gain access to core WordPress functions, but there are much better ways to do this. It's best if you tie your processing functions (the ones that need but don't have access to core functions) into an action hook, such as "init" or "admin_init".

我还尝试在主题和调用页眉和页脚中移动此文件"foo_get_posts.php",但它给了我错误,所以我恢复了我的工作。

如果我从文件中删除require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php');这一行,我没有得到我的数据库结果,所以这对我来说是必要的。

所以请告诉我如何解决这个问题,请帮助我。

根据wordpress文档,如果你的插件将被称为"神话般的功能",你可以称你的PHP文件为神话般的功能.php。因此,首先创建该文件并包含其他文件