How to add alt attribute of images in all of my drupal articles or other content type

March 08, 2018

I have a custom content type, and there are around 2000 number of nodes in my website. In all the nodes, there was no alt attribute in the images. And, it was one of the major SEO disaster that I was facing.

I started updating them one by one. I updated around 10, and I thought of automating that. And, I did it.

Small Introduction

I was using drupal 7, and I wanted to have my alt tags similar to the title of node.

The Code

```php $node_type = "your content type name, e.g. article"; $result = db_query("SELECT nid FROM node WHERE type = :nodeType ", array(':nodeType'=>$node_type));

$done=0; foreach ($result as $obj) { // I wanted to update 10 articles at once, just to avoid php timeout // Specially, when you have many nodes if ($done >= 10) { break; } $nd = node_load($obj->nid); if ( //field_image_of_drawing is the name of field in the content type $nd->field_image_of_drawing[‘und’][0][‘alt’] === null || $nd-> field_image_of_drawing[‘und’][0][‘alt’] === ” || !isset($nd-> field_image_of_drawing[‘und’][0][‘alt’]) ) { ++ $done;

    //just printing node id
    print $obj->nid . ', ';

    $nd-> field_image_of_drawing['und'][0]['alt'] = $nd->title;
    $nd-> field_image_of_drawing['und'][0]['title'] = $nd->title;

    //updating the node


Similar Posts

Latest Posts