wp-config.php
应始终已加载,且不需要包含。
在大多数情况下,确保$wbdp
包含在插件和主题函数中,以确保在正确的位置找到主题和插件文件,并且插件或主题处于活动状态。
如果global $wpdb
尚未设置,则不需要包含任何核心WordPress文件。这很可能意味着您的函数被触发得太早,您应该将其放置在一个动作中,例如muplugins_loaded
, plugins_loaded
, after_setup_theme
, init
, 或wp_loaded
.
如果您有一个单独的文件,像ajax请求一样使用,那么您应该将其切换出来,改用WordPress ajax挂钩。
我发现的唯一有效时间包括wp-config.php
是在普通的基础上扩展if ( ! defined( \'ABSPATH\' ) ) exit;
, 这样,当用户试图直接访问我的任何主题或插件文件/目录时,都会被重定向到主页。
<?php
/**
* Silence is golden, but I actually want people to view the website.
*/
if ( ! defined( \'ABSPATH\' ) ) {
// Call dirname() 4 times as this file is in \'/wp-contents/plugins/plugin-dir/\' and wp-config.php is in \'/\'
require_once dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . \'/wp-config.php\';
// Permanent redirect home.
wp_safe_redirect( home_url(), 301 );
die;
}