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

Samad
  • 1,776
  • 2
  • 20
  • 35

3 Answers3

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.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • almost, except that wordpress isnt always installed in document_root - they have a function get_home_path() that is generally used, just incase – Jonathan Oct 16 '14 at 21:39
  • 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. – Niels Keurentjes Oct 16 '14 at 21:41
  • 1
    I jsut edited my comment, it appears that BASE_PATH isnt available any more and http://codex.wordpress.org/Function_Reference/get_home_path is the new preffered method :( – Jonathan Oct 16 '14 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. – Niels Keurentjes Oct 16 '14 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 '14 at 21:43
  • As the saying goes in my company - if you keep using Wordpress there's no way to unbork it :P – Niels Keurentjes Oct 16 '14 at 21:44
  • is get_home_path a php function or wordpress function? – Samad Oct 16 '14 at 21:49
  • 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. – Niels Keurentjes Oct 16 '14 at 21:51
  • I need a php function – Samad Oct 16 '14 at 21:56
  • 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. – Niels Keurentjes Oct 16 '14 at 21:58
  • 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 script – Jonathan Oct 16 '14 at 22:00
  • 1
    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. – Niels Keurentjes Oct 16 '14 at 22:01
  • if you make an edit then I can remove the down vote since its a good answer :D – Jonathan Oct 16 '14 at 22:03
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';
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • 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 05 '20 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');
Jonathan
  • 1,542
  • 3
  • 16
  • 24
  • Fatal error: Call to undefined function get_home_path() – Samad Oct 16 '14 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 '14 at 22:01