Run one time PHP code on Drupal

Many times while searching for solutions for my Drupal problems I see snippets of code that do various things. How do I run them?

There are several option:

  1. Create a custom module
  2. Bootstrap Drupal and then run the code
  3. Use template.php

What I suggest is much easier – if you have the “Devel” module installed and enabled you can use:
http://mydomain.com/devel/php  there you’ll get a textarea field to paste and run your code.

Here is some PHP code I used to add an FAQ item to each product node:

Another similar code – this time checking the subfamily the product belongs to before updating FAQ items:

The code below is used to go over all products and replace an accessory or cart accessory entity with another:

Another code showing how to add an accessory (to both product accessories and cart accessories) if it doesn’t exist already:

Same as above, but add accessory only if the product has a price (product for sale):

The next code shows how to remove an item from an array of values for a specific field:

The following example check if a product is for sale, if so check its weight. If set to 0, change to 1:

Here is a code that goes over all nodes of type “blog_post” and changes the link structure for the blog post (blog moved to a sub-domain):

Leave a Reply

Your email address will not be published. Required fields are marked *