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

4
  • 2
    require is a filesystem-local procedure and therefore doesn't pass the .htaccess rules. Oct 16, 2014 at 21:32
  • then why I got that error?!
    – Samad
    Oct 16, 2014 at 21:34
  • Probably a problem with your file paths. Oct 16, 2014 at 21:34
  • 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?!
    – Samad
    Oct 16, 2014 at 21:41

3 Answers 3

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.

13
  • 2
    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. Oct 16, 2014 at 21:41
  • 1
    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 :(
    – Jonathan
    Oct 16, 2014 at 21:42
  • 1
    That's one of the reasons I'd greatly prefer relative paths based on __DIR__ - Wordpress and its crappy inconsistent APIs. Oct 16, 2014 at 21:43
  • 1
    yeah, Ill need to modify a few themes when I get to work tomorrow as I just realised they are probably all borken :o
    – Jonathan
    Oct 16, 2014 at 21:43
  • 1
    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. Oct 16, 2014 at 21:58
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';
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.
    – noway
    Jun 5, 2020 at 17:27
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');
2
  • Fatal error: Call to undefined function get_home_path()
    – Samad
    Oct 16, 2014 at 21:53
  • I've edited my answer - you may want to include some more info in your question about including wordpress core into an external script...
    – Jonathan
    Oct 16, 2014 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.