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

时间:2014-11-27 10:46:00

标签: php jquery ajax wordpress wordpress-plugin

我在插件上工作,我在插件中创建了一个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');,所以我不能得到我的结果,所以我问question这个,现在我的工作完美无缺。< / p>

问题

现在我的插件已经完成,我将它发送给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".

我也试着移动这个文件&#34; foo_get_posts.php&#34;在主题和调用页眉和页脚,但它给了我错误,所以我恢复了我的工作。

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

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

1 个答案:

答案 0 :(得分:1)

根据wordpress文档,如果你的插件被称为“神奇功能”,你可以将你的PHP文件称为fabulous-functional.php。所以首先创建该文件并包含其他文件