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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26414230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:53:38  来源:igfitidea点击:

how to allow require_once() to php file in wordpress

phpwordpress.htaccessrequire-once

提问by Samad

I have a web site made by wordpress and I made some php files that i want to execute and for some reason I need to require_once(/wp-includes/class-phpass.php) but I got Failed opening required Error, there is a htaccess file in root folder and it doesn't exist in wp-includes folder the htaccess contain this:

我有一个由 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

so how to solve this problem?! , Thanks

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

Edit

编辑

my wordpress is not installed in the root folder it's like root/live

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

回答by Niels Keurentjes

Assuming this is your literal code:

假设这是您的文字代码:

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

No wonder the file can't be found, as requireoperates on the filesystem level, so you probably need something like /var/www/mysite/wp-includes/class-phpass.phpinstead.

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

You should be able to get it work like this:

你应该能够让它像这样工作:

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

This inserts the current root path of the web site before the subpath. $_SERVER['DOCUMENT_ROOT']is by default the only semblance PHP has of a 'root path' unless you teach it better.

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

回答by Alessandro Ornano

Wordpress 5.x compatible:

Wordpress 5.x 兼容:

This can be used for example to functions.phpof your theme:

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

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

  require_once MY_THEME_DIR.'includes/bs4navwalker.php';

回答by Jonathan

As mentioned in the comment, require is a filesystem-local procedure - it doesn't process the htaccess rules.

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

You are trying to

你正试图

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

this is looking in your machines root for /wp-includes/

这是在您的机器根目录中查找 /wp-includes/

This would work if your wordpress is installed in the document_root (burt is not the recommended way):

如果您的 wordpress 安装在 document_root 中,这将起作用(burt 不是推荐的方式):

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

But you should use this:

但是你应该使用这个:

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

as referenced from this codex page: http://codex.wordpress.org/Function_Reference/get_home_path

从此法典页面引用:http: //codex.wordpress.org/Function_Reference/get_home_path

If you are making scripts that need to use the wordpress core, but aren't executed from within the scope of wordpress itself, then you would need to do the following:

如果您正在制作需要使用 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');