1

I have constructed a custom gallery type as a new shortcode in Wordpress.

However I want to make it a little more SEO friendly so am trying to call the alt tag, and if the alt tag is not found replace it with the title.

So far I have the following (and it works fine) but I am stuck at trying to have $alt fallback to title if $alt is empty? Any ideas/help appreciated!

$bigimageUrl = wp_get_attachment_image_src($attachment_id,$size='full-size');
$littleimageUrl = wp_get_attachment_image_src($attachment_id,$size='thumbnail');
$title_raw = $attachment->post_title;
$title = preg_replace("/[^a-zA-Z0-9\s]/", " ", $title_raw);
$alt_raw = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$alt = preg_replace("/[^a-zA-Z0-9\s]/", " ", $alt_raw);

echo '<img class="gallerytype" title="'; echo $title; echo '" alt="'; echo $alt; echo '" data-rsTmb="'; echo $littleimageUrl[0]; echo '" src="'; echo $bigimageUrl[0]; echo '"/> ';
tjh
  • 89
  • 1
  • 6
  • 1
    possible duplicate of [How to parse and process HTML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) - your problem is that you use regular expressions for that. You need to learn how to parse and process HTML properly first, see the related link. – hakre Sep 28 '12 at 10:05
  • @hakra much appreciated! Will give it a read. As you can tell still wrestling with PHP – tjh Sep 28 '12 at 10:28
  • My fault, I placed you the wrong link. – hakre Sep 28 '12 at 10:32
  • @SMacFadyen to my shame I didn't even realise that it was an option, I was merrily up-voting past ones that worked and leaving it at that – tjh Sep 28 '12 at 14:13

1 Answers1

1

Instead of using one echo after the other:

echo '<img class="gallerytype" title="';
echo $title; 
echo '" alt="'; 
echo $alt; echo '" data-rsTmb="'; 
echo $littleimageUrl[0]; 
echo '" src="'; 
echo $bigimageUrl[0]; echo '"/> ';

You can use a comma (,) to separate expressions to be output:

echo '<img class="gallerytype" title="', $title, '" alt="', ..., '"/> ';

That should already help you to make the code a little mit more readable.

Then you want to set $alt to $title if it is empty. So do that before the output:

empty($alt) && $alt = $title;

Read as: $alt is empty and $alt is $title.

You could also write that with an if clause:

if (empty($alt)) $alt = $title;

That should do it for you.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Works perfectly. Thanks for taking the time to do this. Appreciated – tjh Sep 28 '12 at 10:41
  • 1
    If you're learning PHP @tjh I'd recommend that you use the `if` statement hakra has written for you, rather than the ternary operator. Using an `if` statement makes much more logical sense to users new to PHP, as you can actually read it out loud clearly in your head: `if $alt is empty, alt equals title`. The ternary operator on the other hand (`empty($alt) && $alt = $title;`) makes a little less sense and makes it harder to debug, especially if you come back to the code after a period of time away from it. – turbonerd Sep 28 '12 at 11:13
  • @dunc I prefer to think of it as battling PHP rather than learning it... but yes, advice appreciated, will do this – tjh Sep 28 '12 at 14:11