WordPress.org

Forums

Contextual Related Posts
Suggestion: Limit characters in content that is compared (4 posts)

  1. stevemagruder
    Member
    Posted 2 years ago #

    As a performance suggestion, I would recommend having a character limit for the content that is used to do the comparison queries. This would be set in "General options". Perhaps '0' or blank could mean "no limit".

    http://wordpress.org/extend/plugins/contextual-related-posts/

  2. Ajay
    Member
    Plugin Author

    Posted 2 years ago #

    I think I can add this function. I understand you mean search within the first X words of the current post when fetching the results?

  3. stevemagruder
    Member
    Posted 2 years ago #

    That would work too. Although a character limit can work as well if you do something like the following:

    function max_formatted_content($Content, $MaxLength = -1) {
      $TheContent = str_replace("\r", "", str_replace("\n", " "), $TheText));  // Remove CRLFs, leaving space in their wake
    
      if (($MaxLength > 0) && (strlen($Content) > $MaxLength)) {
        $aWords = preg_split("/[\s]+/", substr($Content, 0, $MaxLength));
    
        // Break back down into a string of words, but drop the last one if it's chopped off
        if (substr($Content, $MaxLength, 1) == " ") {
          $TheContent = implode(" ", $aWords);
        }
        else {
          $TheContent = implode(" ", array_slice($aWords, 0, -1));
        }
    
        $TheContent = close_open_tags($TheContent);
      }
    
      return $TheContent;
    }

    Here's the close_open_tags function it calls:

    function close_open_tags($html, $ignore=array('img', 'hr', 'br')) {
      if (preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $opentags)) {
        $opentags[1] = array_diff($opentags[1], $ignore);
        $opentags[1] = array_values($opentags[1]);
        preg_match_all("#</([a-z]+)>#iU", $html, $closetags);
        $opened = count($opentags[1]);
        if (count($closetags[1]) == $opened) return $html;
        $opentags[1] = array_reverse($opentags[1]);
        for ($i=0;$i<$opened;$i++) {
          if (!in_array($opentags[1][$i], $closetags)) $html .= '</'.$opentags[1][$i].'>';
          else unset($closetags[array_search($opentags[1][$i], $closetags)]);
        }
      }
    
      return $html;
    }
  4. Ajay
    Member
    Plugin Author

    Posted 2 years ago #

    Thanks for the functions steve

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Contextual Related Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.