最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Is it OK to include files using ABSPATH?

programmeradmin1浏览0评论

I was using the following: get_home_path(); in my functions.php as referenced in the docs – but I get the error:

Call to undefined function get_home_path() in ...

I'd like to include a file using it. I noticed there is also the constant ABSPATH. Is there any reason I shouldn't just use ABSPATH as an alternative to get_home_path()?

Edit

I am using it as so (this is crazy simple) – in my functions.php file at the start of the file I have put:

require_once( ABSPATH . 'vendor/autoload.php' );

If I put:

$path = get_home_path();
require_once( $path . 'vendor/autoload.php' );

That's when it all goes wrong, with the generic error about the function not being available.

I was using the following: get_home_path(); in my functions.php as referenced in the docs – but I get the error:

Call to undefined function get_home_path() in ...

I'd like to include a file using it. I noticed there is also the constant ABSPATH. Is there any reason I shouldn't just use ABSPATH as an alternative to get_home_path()?

Edit

I am using it as so (this is crazy simple) – in my functions.php file at the start of the file I have put:

require_once( ABSPATH . 'vendor/autoload.php' );

If I put:

$path = get_home_path();
require_once( $path . 'vendor/autoload.php' );

That's when it all goes wrong, with the generic error about the function not being available.

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Sep 23, 2015 at 10:14 DjaveDjave 2584 silver badges25 bronze badges 3
  • What is your exact usecase. I cannot really guess why you would get the error you are getting. Also, ABSPATH is used by get_home_path() if you look at the source code – Pieter Goosen Commented Sep 23, 2015 at 10:32
  • I'm using composer to install some dependencies of my Wordpress install. – Djave Commented Sep 23, 2015 at 12:18
  • Shouldn't you be working within your theme folder? So have composer & vendor set up inside wp-content/themes/your-theme – TheDeadMedic Commented Sep 23, 2015 at 13:55
Add a comment  | 

2 Answers 2

Reset to default 1

get_home_path() only works when WP admin scripts are loaded.

ABSPATH is defined in wp-config.php so works anytime after the config file is loaded, so it should be totally fine for your use.

It is set to the dirname(__FILE__) + trailing slash of wp-load.php (same location as e.g. wp-config.php). dirname here returns the absolute path where WordPress is installed. For example, /home/user/public_html and ABSPATH adds a trailing slash

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

// So,
ABSPATH == '/home/user/public_html/'; // WP files are in public_html

So if you have a folder structure such as this:

- public_html/
- - wp-admin/
- - wp-content/
- - wp-includes/
- - wp-config.php
- - wp-load.php
- - vendor/
- - - autoload.php
- ...

You can put the following in your theme's functions.php file to load the autoload.php file

require_once( ABSPATH . 'vendor/autoload.php' );

Perhaps this issue is that get_home_path() can only be used in the admin? As it says in small letters in the docs:

This is a backend function.

发布评论

评论列表(0)

  1. 暂无评论