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?
3
-
Possible duplicate: stackoverflow.com/questions/5014244/…Laxman13– Laxman132011-06-29 14:16:38 +00:00Commented Jun 29, 2011 at 14:16
-
2I strongly recmmend that you consider writing a module instead of adding arbitrary PHP files for things that should be part of the website. And for scripts, you can write drush scripts (drupal.org/project/drush).Berdir– Berdir2011-06-29 16:21:51 +00:00Commented Jun 29, 2011 at 16:21
-
@Berdir I am using it for testing purpose.Dijo David– Dijo David2011-06-30 05:25:28 +00:00Commented Jun 30, 2011 at 5:25
Add a comment
|
4 Answers
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);
2 Comments
ErichBSchulz
this looks like a hybrid of both drupal 6 and drupal 7
sandykadam
How do we do for Drupal8?
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.
Comments
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
print_r($user);
1 Comment
Tom Fenech
This question already has an accepted answer. If your answer is an improvement, you should add some detail explaining why.