Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Run PHP script using drupal bootstarp on drupal site

By editor , 11 October, 2019
Question

How to run a php script to execute actions on a drupal site ?

This is usefull if you want to do some actions just one time.

The drush comman is php-script alias src

On drupal 8 with drush

Example :
drush --uri=drupal.loc scr /modules/custom/mymodule/scripts/test-script.php

Example:
<?php
$nid = 10;
$node = \Drupal\node\Entity\Node::load($nid);
$node->setTitle('The new Title')
$node->save();

 


On drupal 7 without drush command

1. put the script to the drupal root folder.
2. run using 'php cli'

Example:
php cli test-script.php [NID]

Example:
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Delete a node
$nodeId = 10;
echo "\r\nNode ID = $nodeId";
node_delete($nodeId);
echo "\r\n";
echo "\r\n";

 

Example 2

<?php
define('DRUPAL_ROOT', getcwd());
$_SERVER['SCRIPT_NAME'] = '/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/path/to/this/script.php';
$_SERVER['HTTP_HOST'] = 'domain.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Delete a node
$nodeId = $argv[1];
echo "\r\nNode ID = $nodeId";
node_delete($nodeId);
echo "\r\n";
echo "\r\n";

Tags

  • Drupal 8
  • Code
  • PHP

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer