Solved

Is there a way we can set Alt Tags for images automatically in a Drupal site

Posted on 2012-12-29
7
1,065 Views
Last Modified: 2013-01-17
Is there a way we can set Alt Tags for images automatically in a Drupal site
I heard Wordpress has a plugin and is there any module to do this?
0
Comment
Question by:bsharath
7 Comments
 
LVL 12

Assisted Solution

by:junipllc
junipllc earned 200 total points
ID: 38730279
I depends on where your images are coming from, i.e. what is the code or module that is displaying the image? This would be similar to using timthumb or another plugin in WP. By default, all of the modules I can think of can do it. Of course, if you're putting your own images into the content using straight HTML and not via output from a module then you can edit the attributes right in the editor.

If you're not sure how you're getting the images, can you provide a description of how it's accomplished in WP? It'll be easier to guide you in the right direction.

I wish I had a better way to articulate what I mean. I hope you understand what I'm asking for. If not, ping back. It's late at night here so at the moment not all brain cells are firing 100%. :)

Mike
0
 
LVL 16

Assisted Solution

by:HagayMandel
HagayMandel earned 200 total points
ID: 38730483
This request pops up several questions:
1. Are you referring to ALL images in ALL content, or you're limiting the request to specific content type, comments, user roles etc.
2. What should be the 'automatic' content for these a title (alt) tags? Will it be the image file name, the content type name, a default fixed value, the related taxonomy term (if so, which one) ?
Generally, as junipllc indicated, all module-generated images can be manipulated to display title tags. Image fields added to content type can be configured to do so.
0
 
LVL 11

Author Comment

by:bsharath
ID: 38730536
Yes all images
My site has many images that are posted by visitors and i want the images to get the Alt tag may be the post title can be added

Sitre name : http://tinyurl.com/62tgwwq
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Assisted Solution

by:HagayMandel
HagayMandel earned 200 total points
ID: 38730611
The easiest solution (without creating a module or preprocessing)

In your theme directory create a 'js' folder, and in it create a page named 'functions.js'.
In functions.js put the following:
jQuery(document).ready(function($){
             $('#content img').attr('title', $('#page-title').text());
});

Open in new window


Save the file.

In your theme .info file, add the following line:

  scripts[] = js/functions.js

Open in new window


That's it: Any image will get its node title as title (alt) tag.
0
 
LVL 12

Accepted Solution

by:
junipllc earned 200 total points
ID: 38731067
As a general rule, I typically don't argue other Experts' solutions, but I have to add this one since I feel it's important.

jQuery is an interesting solution, but with jQuery used that way, none of the search engines will pick up the ALT tags, (business goal) and neither will screen readers and assistive devices (WCAG/accessibility guidelines).  This is because they don't parse or execute JavaScript or jQuery. The way that code is written, though, will work for adding the TITLE attribute, but not the ALT attribute. So the tooltip would work when hovering over the image for a user using a regular browser. Of course, adding one more line of jQuery could add the ALT as well, but you'd still be in the same position.

The other thing I see is that all images would have the page/node name as the title attribute. ALT and TITLE tags are supposed to be descriptive of the image itself, not the page/node. It helps with both of the above goals, business and universal access:

Refs:
http://support.google.com/webmasters/bin/answer.py?hl=en&answer=114016
http://www.w3.org/TR/2012/NOTE-WCAG20-TECHS-20120103/H37.html

HagayMandel is right, however, in that without doing preprocessing it's difficult to obtain the goal. However, there is a theme function for it that you can override in your theme. You just need to change the values inside the $attributes array in the function to output all themed images with ALT, TITLE, or any other attribute (Drupal 7, but there is a link there for Drupal 6):

http://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_image/7

Basically, you'd copy the theme_image() code into your template.php file, rename it to match your theme (see below) and add a little code to modify how alt tags are set. Voilà! All of your alt tags should be set (be sure to clear the Drupal cache before testing):

function techpedia_image($variables) {
  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }

  // NEW code to set alt tags that aren't already set (not verified to be correct):
  if (!isset($attributes['alt'])) {
    // do something here to set the alt tag
  }

  return '<img' . drupal_attributes($attributes) . ' />';
}

Open in new window


Cheers,

Mike
0
 
LVL 16

Expert Comment

by:HagayMandel
ID: 38731089
@bsharath:

Go for junipllc's solution, it requires a bit more code playing, but it's a better approach, and you'll get better results.
Still, the questions that were asked, still valid: What will you get of this procedure, and what it's good for?
If it's SEO better performance you're after, I'm afraid, that multiple node titles as images' alt and title tags, will lead you to the dark side of penalties and down ranking.
Anyhow, good luck.

Hagay
0
 
LVL 9

Assisted Solution

by:oliverpolden
oliverpolden earned 100 total points
ID: 38754611
Actually there is the Imagefield Tokens module. There's a Drupal 7 dev version which I have tested and works fine:
http://drupal.org/project/imagefield_tokens

You need to enable the token module as well. Then set the Alt attribute to use the Title and the title to use the [node:title] token as its default. You can also set when to update them. I just set them both to update on every save. This is done in the field settings for the image within your content type.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now