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

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…