how to allow require_once() to php file in wordpress
Asked Answered
U

3

5

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:

# 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

Unborn answered 16/10, 2014 at 21:30 Comment(4)
require is a filesystem-local procedure and therefore doesn't pass the .htaccess rules.Firearm
then why I got that error?!Unborn
Probably a problem with your file paths.Holarctic
my root directory is like this /index.php . . . /myfile.php /wp-includes/class-phpass.php and the function is require_once( '/wp-includes/class-phpass.php' ); is there wrong thing?!Unborn
F
18

Assuming this is your literal code:

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

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

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.

Firearm answered 16/10, 2014 at 21:38 Comment(13)
almost, except that wordpress isnt always installed in document_root - they have a function get_home_path() that is generally used, just incaseAlkanet
Yeah well I avoid Wordpress like the plague so I don't know all of its custom constants. I explicitly stated 'should be able to get it to work' because it was likely in the root. You should still, also if BASE_PATH is available, prefer relative paths based on __DIR__ instead. But I'll delete this answer as yours is more complete for WP.Firearm
I jsut edited my comment, it appears that BASE_PATH isnt available any more and codex.wordpress.org/Function_Reference/get_home_path is the new preffered method :(Alkanet
That's one of the reasons I'd greatly prefer relative paths based on __DIR__ - Wordpress and its crappy inconsistent APIs.Firearm
yeah, Ill need to modify a few themes when I get to work tomorrow as I just realised they are probably all borken :oAlkanet
As the saying goes in my company - if you keep using Wordpress there's no way to unbork it :PFirearm
is get_home_path a php function or wordpress function?Unborn
If the documentation is on codex.wordpress.org and not on www.php.net I would expect it to be a WP function. PHP does not know the concept of 'home paths' anyway by definition.Firearm
I need a php functionUnborn
PHP doesn't know the concept of 'home paths'. A PHP file is just that - a file. It has no concept of a home path unless it is taught to, that's exactly why WP has this function for it.Firearm
also, it turns out that parts of your answer are better suited, I think the OP is trying to include parts of wordpress in an external scriptAlkanet
Well as long as it has a negative score I'm going to delete it anyway. If that is the intent behind the question it's too vague to be useful.Firearm
if you make an edit then I can remove the down vote since its a good answer :DAlkanet
C
2

Wordpress 5.x compatible:

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

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

  require_once MY_THEME_DIR.'includes/bs4navwalker.php';
Confiteor answered 27/3, 2020 at 16:17 Comment(1)
Please note if using child themes this will return the path of the parent theme folder. get_stylesheet_directory() seems to be more error-proof.Birdcage
A
0

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

You are trying to

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

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

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

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

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:

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');
Alkanet answered 16/10, 2014 at 21:37 Comment(2)
Fatal error: Call to undefined function get_home_path()Unborn
I've edited my answer - you may want to include some more info in your question about including wordpress core into an external script...Alkanet

© 2022 - 2024 — McMap. All rights reserved.