When I execute this code in functions.php
I get the path:
$upload_dir = wp_upload_dir();
echo $upload_dir['path'];
//Returns: /some/correct/path/
But I want this path to be moved to my custom script located in uploads.php
where I can't execute wp_upload_dir();
. I obviously can't include()
functions.php either because it would execute everything else that is located there.
Is there any tricky way to pass $upload_dir
variable to uploads.php
?
You just need to load wp-load.php it will give you access to all wordpress functions, just do something like this:
<?php
require( '../wordpress_install_root/wp-load.php' );
$upload_dir = wp_upload_dir();
echo $upload_dir['path'];
?>
This is an easy way.
just include the following at the top of your external php file:
$wp_url = str_replace('wp-content/plugins/YOUR_PLUGIN_NAME/includes', "", dirname(__FILE__));
include_once($wp_url . 'wp-load.php');
obviously replace YOUR_PLUGIN_NAME with the name of your plugin. I use it in a single file that is being called by Paypal and other payment gateways.
Some also use the following but i never got it to work properly and I'm unsure if it is deprecated.
require_once('../../../../wp-blog-header.php');