简体   繁体   中英

Drupal 7.x: how to create a new node with PHP input format?

I want execute custom PHP script that will delete all nodes of a particular type (Feed items). How to create a new node with PHP input format, so I can paste code into the node body? Please specify detailed steps.

$node_type = 'Feed items';

  //fetch the nodes we want to delete
  $result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);
  while ($row = db_fetch_object($result)){
    node_delete($row->nid);
    $deleted_count+=1;
  }
  //simple debug message so we can see what had been deleted.
  drupal_set_message("$deleted_count nodes have been deleted");

It's not entirely clear what you want to achieve. Do you simply want to execute that PHP code, once? Or whenever a node of a certain type is created?

If you just want to execute the PHP code once, you can install the devel module ( http://drupal.org/project/devel ) and paste your code in <your_site_url>/devel/php

--EDIT

Another option would be to create a php page that bootstraps Drupal and runs your code: http://www.avenuewebmedia.com/drupalbootstrap-connect-drupal-external-php-script

OK, I managed myself how to do this: I had to enable the "PHP filter" module first (in Drupal core, disabled by default). In admin > Configuration > Content authoring > Text formats > Add text format, in "Enabled filters" mark "PHP evaluator", in Roles select 'admin', save configuration. Then I created a new node (article), set the input format to PHP, pasted the code into the node body, added a title and clicked submit. Code has been executed. Sure, we need to delete the node when done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM