Customizing SMF > SMF Coding Discussion

Error when trying to add "alt" to images

(1/1)

r2017:
Hello!

I wanted to have image "alt". To do this, the file Subs.php replaced code:


--- Code: ---  array(
     'tag' => 'img',
     'type' => 'unparsed_content',
     'parameters' => array(
      'alt' => array('optional' => true),
      'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'),
      'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)'),
          ),
     'content' => '<img src="$1" alt=" {alt}"{width}{height} border="0" />',
     'validate' => create_function('&$tag, &$data, $disabled', '$data = strtr($data, array(\'<br />\' => \'\'));'),
     'disabled_content' => '($1)',
    ),
   array(
     'tag' => 'img',
     'type' => 'unparsed_content',
     'content' => '<img src="$1" alt="" border="0" />',
     'validate' => create_function('&$tag, &$data, $disabled', '$data = strtr($data, array(\'<br />\' => \'\'));'),
     'disabled_content' => '($1)',
    ),
--- End code ---

This:

--- Code: ---  array(
     'tag' => 'img',
     'type' => 'unparsed_content',
     'parameters' => array(
      'alt' => array('optional' => true),
      'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'),
      'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)'),
   
     ),
     'content' => '<img src="$1" alt="' . $context['page_title'] . ' {alt}"{width}{height} border="0" />',
     'validate' => create_function('&$tag, &$data, $disabled', '$data = strtr($data, array(\'<br />\' => \'\'));'),
     'disabled_content' => '($1)',
    ),
    array(
     'tag' => 'img',
     'type' => 'unparsed_content',
     'content' => '<img src="$1" alt="' . $context['page_title'] . '" border="0" />',
     'validate' => create_function('&$tag, &$data, $disabled', '$data = strtr($data, array(\'<br />\' => \'\'));'),
     'disabled_content' => '($1)',
    ),

--- End code ---

It works. As an "alt" displays the title of the topic. However, errors in the logs were to appear the following messages:


--- Code: ---Undefined index: page_title
File: /home/name/data/www/mydomain.ru/Sources/Subs.php
--- End code ---

As I understand the variable is not declared or something like that.

Russian support SMF advised me to use instead of $context ['page_title']  $context ['page_title_html_safe'] or $context ['subject']. I tried both, but the error continues.

Can you please tell me how to change the code to get rid of the error, or any other way to add "alt" to images.

emanuele:
parce_bbc is used for several things, for example the news too.
It's possible that at a certain point parse_bbc is used, but the page title is not yet defined (as well as a subject).

I'd suggest you to check if it is not empty and then use it or just return an empty string:

--- Code: ---'content' => '<img src="$1" alt="' . (!empty($context['page_title_html_safe']) ? $context['page_title_html_safe'] : '') . ' {alt}"{width}{height} border="0" />',
--- End code ---
BTW, if you are doing this for SEO or similar purposes I don't thing it's a great idea: you will end up with potentially several unrelated images with the same alt tag...don't know how effective this is. But I'm not an expert in the field (well, I don't really care about SEO at all), so my concerns are basically useless. :P

r2017:
Thank you for your help!

My users place a lot of unique images, I would like to receive traffic from the search engines and search for the pictures. Usually the topic title reflects the theme of being in her images. Well, not quite the presence of relevant "alt" attribute in the image is much better for his absence. :)

Navigation

[0] Message Index

Go to full version