如何允许require_once()到wordpress中的php文件中

问题描述 投票:4回答:3

我有一个由wordpress制作的网站,我制作了一些我想执行的php文件,由于某种原因,我需要require_once(/wp-includes/class-phpass.php),但是我无法打开所需的错误信息,是根文件夹中的htaccess文件,在wp-includes文件夹中不存在,htaccess包含以下内容:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

那么如何解决这个问题?! ,谢谢

编辑

我的wordpress没有安装在根文件夹中,就像root / live一样

php wordpress .htaccess require-once
3个回答
12
投票

假设这是您的文字代码:

require_once('/wp-includes/class-phpass.php');

难怪找不到文件,因为require在文件系统级别上运行,所以您可能需要类似/var/www/mysite/wp-includes/class-phpass.php的东西。

您应该能够使它像这样工作:

require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/class-phpass.php';

这将在子路径之前插入网站的当前根路径。默认情况下,$_SERVER['DOCUMENT_ROOT']是PHP唯一具有“根路径”的外观,除非您更好地教它。


1
投票

与Wordpress 5.x兼容:

例如,可以用于您主题的functions.php

if (!defined("MY_THEME_DIR")) define("MY_THEME_DIR", trailingslashit( get_template_directory() ));

  require_once MY_THEME_DIR.'includes/bs4navwalker.php';

0
投票

如评论中所述,require是一个文件系统本地过程-它不处理htaccess规则。

您正在尝试

require_once(/wp-includes/class-phpass.php);

这正在您的计算机根目录中查找/ wp-includes /

如果您的wordpress已安装在document_root中(不建议使用burt,则可以使用:]

require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-includes/class-phpass.php');

但是您应该使用此:

$install_path = get_home_path();
require_once($install_path. '/wp-includes/class-phpass.php');

从此抄本页面引用:http://codex.wordpress.org/Function_Reference/get_home_path

如果您要编写的脚本需要使用wordpress核心,但不会在wordpress本身的范围内执行,那么您需要执行以下操作:

define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require( $_SERVER['DOCUMENT_ROOT'] . '/path/to/wp-load.php');

$install_path = get_home_path();
require_once($install_path. '/wp-includes/class-phpass.php');
© www.soinside.com 2019 - 2024. All rights reserved.