Calling Drupal functions in external PHP file
Solution 1
Taken from the linked question in the comment above
You need to Bootstrap Drupal in the external PHP file:
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Be sure to change the path to your Drupal installation, then add your code below the code posted above.
Solution 2
If the above explained example doesn't work try this:
$path = $_SERVER['DOCUMENT_ROOT'];
chdir($path."/drupal");
define('DRUPAL_ROOT', getcwd()); //the most important line
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Solution 3
define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
The above code works for me, when the script is in my Drupal root directory. This loads absolutely everything, not just Drupal core, including contributed module hooks.
Dijo David
I m a Tech fanatic.. Loves computers. Writes codes. Spend life in Web!! ;)
Updated on June 09, 2022Comments
-
Dijo David almost 2 years
How can I call a Drupal function or get the global variable in a PHP file which is located under the drupal installation folder. I doing it for the first time. Are there any files I need to include in my code in order to access the Drupal function or variables?
-
ErichBSchulz almost 11 yearsthis looks like a hybrid of both drupal 6 and drupal 7
-
Tom Fenech about 10 yearsThis question already has an accepted answer. If your answer is an improvement, you should add some detail explaining why.
-
sandykadam almost 3 yearsHow do we do for Drupal8?