简体   繁体   中英

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

I working on a plugin, in plugin i create an AJAX (in plugin index file) that when a user write something in search bar the related posts title shows in drop down and for get that result i create an other file for this like "foo_get_posts.php" in plugin root directory. Here is my code:

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 file code:

<?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
    }
}
?>

First i don't call require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php'); in my file, so i don't get my results, so i ask a question for this and now my work goes perfectly.

Problem

Now my plugin is complete and i send it to wordpress and they reject it, they say:

## 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".

I also try move this file "foo_get_posts.php" in theme and call header and footer but it gives me error, so i reverted my work.

If i remove this line require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php'); from my file i don't get my DB results, so it is neccessry for me.

So please tell me how can i resolve this issue, please help me.

As per wordpress document,, if your Plugin will be called "Fabulous Functionality", you might call your PHP file fabulous-functionality.php. So first create that file and include other files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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