drupal|March 08, 2018|1 min read

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

TL;DR

Automate adding alt tags to images in Drupal 7 nodes by loading each node, parsing its body HTML, and setting alt attributes to the node title.

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

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
    node_save($nd);
}

}

Related Posts

Drupal Code: Fetch Link, Title, Category names, tag names from every article

Drupal Code: Fetch Link, Title, Category names, tag names from every article

See the code below: The output will be 4 columns separated by comma. You can…

Drupal: How to block a user by email programatically

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

How to trim Meta Description tag value generated by Metatag module, to max 160 characters

How to trim Meta Description tag value generated by Metatag module, to max 160 characters

I was using On page optimization of the article pages, and found that meta…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

Drupal 8: How to Export and Import View

Drupal 8: How to Export and Import View

You have created some views, and want to port it to your production environment…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…