Index: glossary.php =================================================================== --- glossary.php (revision 589602) +++ glossary.php (working copy) @@ -84,6 +84,16 @@ } add_action('wp_print_styles', 'red_glossary_css'); +// Sort longer titles first, so if there is collision between terms (e.g., +// "essential fatty acid" and "fatty acid") the longer one gets created first. +function sortByWPQueryObjectTitleLength($a, $b) { + $sortVal = 0; + if (property_exists($a, 'post_title') && property_exists($b, 'post_title')) { + $sortVal = strlen($b->post_title) - strlen($a->post_title); + } + return $sortVal; +} + function red_glossary_parse($content){ //Run the glossary parser @@ -97,6 +107,9 @@ 'orderby' => 'title' )); + // Sort by title length (function above) + uasort($glossary_index, 'sortByWPQueryObjectTitleLength'); + //the tag:[glossary_exclude]+[/glossary_exclude] can be used to mark text will not be taken into account by the glossary if ($glossary_index){ $timestamp = time(); @@ -262,6 +275,10 @@ //old code bug-doesn't take into account href='' takes into account only href="") //$glossary_search = '/\b'.$glossary_title.'s*?\b(?=([^"]*"[^"]*")*[^"]*$)/i'; $glossary_search = '/\b'.$glossary_title.'s*?\b(?=([^"]*"[^"]*")*[^"]*$)(?=([^\']*\'[^\']*\')*[^\']*$)/i'; + // If protected tags option is turned on, make sure we're not replacing terms that are already in anchor tags (esp. anchor tags generated by another glossary term) + if (get_option('red_glossaryProtectedTags') == 1) { + $glossary_search = '/(?)\b'.$glossary_title.'s*?\b(?=([^"]*"[^"]*")*[^"]*$)(?=([^\']*\'[^\']*\')*[^\']*$)(?!<\/a>)/i'; + } $glossary_replace = '$0'; $origContent = $content; @@ -298,17 +315,17 @@ } if (get_option('red_glossaryTermLink') == 1) { - $link_replace = '$1'; + $link_replace = '$1'; } else { - $link_replace = '$1'; + $link_replace = '$1'; } } else { if (get_option('red_glossaryTermLink') == 1) { - $link_replace = '$1'; + $link_replace = '$1'; } else { - $link_replace = '$1'; + $link_replace = '$1'; } }