SMF Support > SMF 2.0.x Support

Dynamic meta tags in SMF

<< < (2/3) > >>

MrPhil:
As it presently stands,

--- Code: ---<meta http-equiv="Content-Type" content="text/html; charset=', $context['character_set'], '" />
<meta name="description" content="', $context['page_title_html_safe'], '" />',
        !empty($context['meta_keywords']) ? '
   <meta name="keywords" content="' . $context['meta_keywords'] . '" />' : '', '
<title>', $context['page_title_html_safe'], '</title>';

--- End code ---

* the title and the description are the page title (typically the topic subject)
* the keywords are any fixed list of keywords (SMF default or something you provided)I can't tell you off the top of my head how to find out what kind of page you're on, but with a little looking around I'm sure you can find out how SMF does it. For the home page, I would think it would be easy enough to add the title text to the keywords list (possibly stripping out superfluous words like "a", "an", "the", "and", etc., and removing duplicates). For boards (listing topics), presumably the board description is available somewhere to add to (or replace) the description text (page title). That would be useful. The board name could be added to (or replace) the existing fixed keyword list and also the description. For a topic, take the first 100 characters or so of the first post as a "teaser" for the description (along with the title/topic subject). If SE listings are always going to show the page title separately, it is unnecessary to include the title in the description.

In all cases, watch the resulting lengths: you want to keep the description under 150 characters or so (possibly less for non-Google). Don't let your keyword list get out of hand, either, or you might face SE penalties. What you might want to do is just glue together all the keyword fragments, replace blanks by commas, explode the list into an array, remove trivial words, change plurals to singular, remove duplicates, and implode the array back into a list. That should of course be done in a separate function. I don't know if it's necessary to include the page title in the keywords, as that's already considered an important piece of searchable text, and Google doesn't pay much attention to the keywords meta tag anyway (other SEs may).

One thing to watch out for is that you know that you have all the desired information at hand at the point where the meta tags need to be output. Make sure some string isn't defined until later! If a given piece of information hasn't been created or read in yet, you would need to either copy the code up to this point, or rearrange the code so that (e.g., the first post has been read in) the information is created earlier. The latter could potentially be a major source of errors, while the former could slow down page creation because database reads are being done twice.

It's not a trivial project, but I would think it could be done. It will have to start off with very careful investigation of what information is available at what points.

nend:
Here is what I have in my index.template.php. Please note though, I forget where my code ends and SMF code starts. So some of these values may not exist in a stock installation.  ;D


--- Code: --- // Page meta description
if (!empty($context['item_data']['description'])) {
$context['meta_description'] = $smcFunc['substr'](censorText(strip_tags(str_replace('<br />', ' ', parse_bbc($context['item_data']['description'])))), 0, 250).'...';
} else if (!empty($context['description'])) {
$context['meta_description'] = $smcFunc['substr'](censorText(strip_tags(str_replace('<br />', ' ', parse_bbc($context['description'])))), 0, 250).'...';
} else if (!empty($context['meta_description'])) {
$context['meta_description'] = $smcFunc['substr'](censorText(strip_tags(str_replace('<br />', ' ', parse_bbc($context['meta_description'])))), 0, 250).'...';
} else {
$context['meta_description'] = $context['page_title_html_safe'] . ' - Bringing you the latest wrestling news and media for TNA, UFC, WWE and more.';
}
--- End code ---

Ok the first if is for Aeva Media, so if you have that it will display the gallery description for each item.

Second if is for, I believe it is the board description. When your inside a board $context['description'] is set to the description of the board. I could be wrong though, I forget allot lately at my advance age.

Third if is I think this is a custom one which I set in some sources to pass descriptions over.

Forth is just display forum name with some additional text.

MoinFaraz:
Hey,

I have replaced  ::


--- Code: ---<meta name="keywords" content="' . $context['meta_keywords'] . '" />' : '', '
--- End code ---

with ::


--- Code: ---';
//Begin Of AutoKeywords

$keystring = str_replace(' ', ',', trim($context['page_title']));
echo '<meta name="keywords" content="', $keystring, '" />';

//End Of AutoKeywords
echo '
--- End code ---

and it ended up with an template parse error. i am not an expert in PHP and need a little help to get rid of the parse error... i am attaching my current index.template.php for your reference.

Thank you in advance for looking into it :)

MrPhil:
No, your actual code doesn't look anything like that. It's

--- Code: --- echo '
<meta http-equiv="Content-Type" content="text/html; charset=', $context['character_set'], '" />
<meta name="description" content="', $context['page_title_html_safe'], '" />', !empty($context['meta_keywords']) ? '
$keystring = trim($context['page_title']);
echo '<meta name="description" content="', $keystring, '" />';
--- End code ---
You start a string if there are meta_keywords, but then have your PHP code to create $keystring within the string. You are apparently looking for

--- Code: --- echo '
<meta http-equiv="Content-Type" content="text/html; charset=', $context['character_set'], '" />
<meta name="description" content="', $context['page_title_html_safe'], '" />';
if (!empty($context['meta_keywords'])) {
  $keystring = str_replace(' ', ',', trim($context['page_title']));
  echo '<meta name="description" content="', $keystring, '" />';
}
--- End code ---

That doesn't sound like a complete job. You would want to break up the keyword list into individual terms and remove trivial words, and recreate the comma list. You'll also want something in the way of some fixed (constant) keywords, such as from the original meta_keywords entry.

MoinFaraz:
Thank you for looking into it.

I have tried to replace current code with the one provided and still template parse error is there.

Could you possibly recheck the code and provide with a working one (the one which will not give parse error)

Thanks :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version