17

if my main.php file is located inside /wp-content/plugins/myplugin/folder/folder/folder/ why including urls.php , located inside /wp-content/uploads/folder/ doesn't work with this path:

include_once("../../../../../uploads/folder/urls.php");

it works only if placed inside same folder as main.php with path ('urls.php');

2 Answers 2

34

you can use content_url() it's located with http://www.example.com/wp-content wp-content folder.

Or for path

you can use WP_CONTENT_DIR it'll located to wp-content folder.

require_once(WP_CONTENT_DIR. 'uploads/folder/urls.php');

Check more info Documentation

4
  • still not sure why the multiple ../ doesn't work but thanks, with the WP_CONTENT_DIR works Oct 12, 2019 at 15:39
  • 2
    @NathanBernard Script is actually executed in other files which including the file. It may be the site index.php, or wp-admin index.php, or anyone. It's dynamic, so ../ or ./ type relative path will get wrong. Try to put $_SERVER[PHP_SELF] into script and echo out, that will see the actual runing file.
    – plus70
    Jun 8, 2020 at 11:04
  • 1
    It seems that WP_CONTENT_DIR doesn't have a trailing slash, at least in my install. Not sure if this is a recent change, but I just got an error trying to load wp-contentvendor/.... Heads up.
    – phatskat
    Feb 1, 2021 at 15:12
  • 1
    On my context (server? WordPress version? phph version?) WP_CONTENT_DIR doesn't have the trailing slash, so I have to write require_once(WP_CONTENT_DIR. '/uploads/folder/urls.php'); Mar 18, 2021 at 6:08
1

It seems strange. Do you have any error message when you run your main.php? Try to check again, maybe a typo somewhere. Do you take help of Text editor auto-completion when calling urls.php?

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.