<?php
/* Parameters */
/**
* &maxWordCount
* integer (25)
* Sets the maximum word count of the description, default is 25 words
*/
$maxWordCount = (!empty($maxWordCount) && is_numeric($maxWordCount)) ? $maxWordCount : 25;
/**
* &id
* integer ($modx->documentObject['id'])
* the id of the page you want a description for, defaults to the
* current page.
*/
$id = (is_numeric($id) && !empty($id)) ? $id : $modx->documentObject['id'];
/**
* &descriptionTV
* string ('')
* The name of a template variable you've set up for the description.
* If you have a separate template variable set up for a description
* you write in, set that with this parameter. If the parameter ends
* up being empty, it will default back to the content. If it is not
* empty then it will display the value of the TV and stop processing.
* In other words, it will print it just as you typed it.
*/
$descriptionTvArray = (!empty($descriptionTV)) ? $modx->getTemplateVarOutput($descriptionTV,$id) : '';
$descriptionTV = $descriptionTvArray[$descriptionTV];
/* Snippet Code */
/**
* Function: getDynaDescription()
* Returns: A string of text ready to be placed in a meta description tag
*/
if (!function_exists(getDynaDescription)) {
function getDynaDescription($text='',$excerpt_length=25)
{
global $modx;
$text = str_replace(']]>', ']]>', $text);
/* remove line breaks */
$text = str_replace("\n",' ',$text);
$text = str_replace("\r",' ',$text);
/* remove entity chars */
//$text = preg_replace('~&.+;~U',' ',$text);
/* remove special MODx tags - chunks, snippets, etc.
* If we don't do this they'll end up expanded in the description.
*/
$text = $modx->stripTags($text);
$words = preg_split ("/\s+/", $text,$excerpt_length+1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
return trim(stripslashes($text));
}
}
if (!empty($descriptionTV)) {
// if the $descriptionTV is not empty then we just print it with
// no extra processing, it's up to you to manage your description
echo $descriptionTV;
} else {
// for everyone else that is lazy, just grab it from the content, yeah!
$content = $modx->getDocument($id,'content');
$content = $content['content'];
echo getDynaDescription($content,$maxWordCount);
}
<?php
/** getDynaDescription snippet
* Creates a full meta description tag or just the content part.
*/
/* Properties */
/**
* &fullTag
* (int 1/0)
* Set to one if you want a full meta tag returned,
* set to 0 if you want just the content and will create the meta
* tag yourself.
*/
/**
* &maxWordCount
* integer (25)
* Sets the maximum word count of the description, default is 25 words
*/
$mwc = $scriptProperties['maxWordCount'];
$maxWordCount = (!empty($mwc) && is_numeric($mwc)) ? $mwc : 25;
/**
* &id
* integer
* the id of the page you want a description for, defaults to the
* current page.
*/
$pid = $scriptProperties['id'];
$pid = (is_numeric($pid) && !empty($pid)) ? $pid : $modx->resource->get('id');
/**
* &descriptionTv
* string ('')
* The name of a template variable you've set up for the description.
* If you have a separate template variable set up for a description
* you write in, set that with this parameter. If the parameter ends
* up being empty, it will default back to the content. If it is not
* empty then it will display the value of the TV and stop processing.
* In other words, it will print it just as you typed it.
*/
if (!empty ($scriptProperties['descriptionTv'])) {
$tv = $modx->getObject('modTemplateVar',array('name'=>$scriptProperties['descriptionTv']));
$descriptionTv = $tv? $tv->getValue($pid) : '';
} else {
$descriptionTv = '';
}
/**
* Function: getDynaDescription()
* Returns: A string of text ready to be placed in a meta description tag
*/
if (!function_exists(getDynaDescription)) {
function getDynaDescription($text='',$excerpt_length=25)
{
global $modx;
$text = str_replace(']]>', ']]>', $text);
/* remove line breaks */
$text = str_replace("\n",' ',$text);
$text = str_replace("\r",' ',$text);
/* remove entity chars */
//$text = preg_replace('~&.+;~U',' ',$text);
/* remove special MODx tags - chunks, snippets, etc.
* If we don't do this they'll end up expanded in the description.
*/
$text = $modx->stripTags($text);
$words = preg_split ("/\s+/", $text,$excerpt_length+1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
return trim(stripslashes($text));
}
}
if (!empty($descriptionTv)) {
// if the $descriptionTv is not empty, use it
$output = $descriptionTv;
} else {
// for everyone else that is lazy, just grab it from the content, yeah!
if ($pid == $modx->resource->get('id') ) { // don't pull another object unless we have to
$content = $modx->resource->getContent();
} else {
$otherResource = $modx->getObject('modResource', $pid);
$content = $otherResource->getContent();
}
$output = getDynaDescription($content,$maxWordCount);
}
if ($fullTag == true) {
$output = '<meta name="description" content="' . $output . '">' . "\n";
}
return $output;
I’ll take a quick stab at it (untested). I tweaked it a little and added a &fullTag property so you wouldn’t have to type in the rest of the tag for each one:
...
I corrected a couple of errors and updated the code above. Try that and let me know. It works for me.
Quote from: BobRay at Jun 09, 2011, 04:14 PM
I corrected a couple of errors and updated the code above. Try that and let me know. It works for me.
work perfect! Great! What you drink?![]()
![]()


It’s available as a package now, called getDynaDescription.
It’s kind of worth looking at just to see what you can do with installation options in Revo.