Script to update Drupal teasers

One of the annoying things about changing the number of characters for teasers in Drupal is that you have to edit and save every single node (page, story etc) for the change to take effect. Thats ok if you only have 10 pages, but what if you have 1000 or even 10,000 pages? Nightmere times.

Well, it doesn't have to be that difficult, because here is a nice little script that will update all of the teasers at once.

The first step is to change the teaser length in admin -> content ->node settings -> length of trimmed posts.

The next step is to copy the following code into a php page and upload it to the root directory of your site. Then run the script and like magic, all the teasers will change to the desired length.

include "includes/bootstrap.inc";
include "includes/common.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$result = db_query("SELECT nid,body,teaser FROM node_revisions");

while ($node = db_fetch_object($result)) {
$teaser = node_teaser($node->body);
db_query("UPDATE node_revisions SET teaser = '%s' WHERE nid = %d", $teaser, $node->nid);
$i++;
echo $i;
}

Thank you for post this. I'm

Thank you for post this.

I'm new on Drupal and I was looking for some like this.

But i have a question, It is normal that after execute the script appears a large chain of numbers?

Thanks in advance for the response.

Hi Francisco, Yes it is

Hi Francisco,

Yes it is normal. The variable $i increases by one each time the while loop runs and $i is echoed out each time, so the chain of numbers is the variable $i being returned to the screen. I find that useful to indicate that the script is running correctly, but feel free to remove the following line if you wish:
echo $i;

Thank you.

Many thanks for the response mate.

Better I leave the script like the original, this way like you said, I can see the script it's running fine.

Thanks for write great things about Drupal.

Post new comment

The content of this field is kept private and will not be shown publicly.