pluginUrl = plugins_url ( '/', __FILE__ ); $this->pluginPath = plugin_dir_path ( __FILE__ ); $this->get_settings (); $this->sorted [] = null; add_action ( 'plugins_loaded', array ( $this, 'init' ) ); } /** * init() */ function init() { add_filter ( 'the_excerpt', array ( $this, 'my_excerpt' ), 1 ); } /** * Store options */ private function get_settings() { $this->options ["minLen"] = get_option ( 'minLen', 50 ); $this->options ["maxLen"] = get_option ( 'maxLen', 150 ); $this->options ["useTitle"] = get_option ( 'useTitle', 1 ); $this->options ["useTags"] = get_option ( 'useTags', 1 ); $this->options ["featured"] = get_option ( 'featured', 1 ); $this->options ["stripAll"] = get_option ( 'stripAll', FALSE ); $this->options ["stripTags"] = get_option ( 'stripTags', array ( "img", "br", "p" ) ); $this->options ["more"] = get_option ( 'more', FALSE ); $this->options ["keepHtmlTags"] = get_option ( 'keepHtmlTags' ); $this->options ["numSentences"] = get_option ( 'numSentences', 2 ); $this->options["skipManual"] = get_option ( 'skipManual', 0 ); } /** * the_excerpt() */ public function my_excerpt() { global $post; if( !$this->options["skipManual"] && '' != $post->post_excerpt ){ echo $post->post_excerpt; return null; } $content = get_the_content (); if ($this->debug) echo '-------
' . $content . '-------
'; $content = $this->strip_the_tags ( $content ); if ($this->debug) echo '-------
' . $content . '-------
'; // Do not analyse short articles if (strlen ( $content ) < $this->options ["minLen"]) { // Short article limit if ($this->options ["featured"] and has_post_thumbnail ()) the_post_thumbnail ( 'medium' ); echo $content = $this->enrich_the_excerpt ( $content ); return null; } // Title based $dotNL = array ( "…", " ...", ". ", "? ", "! " ); $content = str_replace ( $dotNL, "||||", $content ); $phrases = explode ( "||||", $content ); $avg = (strlen ( $content ) / sizeof ( $phrases )); if ($this->debug) echo '-------
' . print_r ( $phrases ); $tags = explode ( " ", trim ( strtolower ( get_the_title () ) ) ); if (sizeof ( $tags ) > 0 && sizeof ( $phrases ) > 0 && $this->options ["useTitle"]) { $temp = $this->build_the_excerpt ( $tags, $phrases, "title", $avg ); } else $tags = null; // Tag based $tags = get_the_tags (); // print_r($tags); if ($tags && $this->options ["useTags"]) { $temp = $this->build_the_excerpt ( $tags, $phrases, "tags", $avg ); } if ($temp) { if ($this->options ["featured"] and has_post_thumbnail ()) the_post_thumbnail ( 'medium' ); echo $this->enrich_the_excerpt ( $temp ); return null; } else { // Short article if ($this->options ["featured"] and has_post_thumbnail ()) the_post_thumbnail ( 'medium' ); echo $this->enrich_the_excerpt ( preg_replace ( $rep, " ", get_the_content () ) ); return null; } } /** * strip_the_tags() */ private function strip_the_tags($content) { if ($this->options ["stripAll"]) $content = preg_replace ( '/<[^>]*>/', " ", $content ); else { $rem = array (); foreach ( $this->options ["stripTags"] as $tag ) if ($tag == "img") array_push ( $rem, '/]+\>/i' ); else array_push ( $rem, '/\<' . $tag . '(\s*)?\/?\>/i' ); $content = preg_replace ( $rem, "", $content ); } return $content; } /** * build_the_excerpt() * * @param string $tags, * $phrases * @return string $temp * */ private function build_the_excerpt(&$tags, $phrases, $options, $avg) { if ($options == "tags") { foreach ( $tags as $tag ) { // if($this->debug) echo $tag->slug; if (strlen ( trim ( $tag->slug ) ) > 2) { $i = 0; foreach ( $phrases as $phrase ) { $txt [$i] = $phrase; $this->sorted [$i] += substr_count ( strtolower ( $phrase ), trim ( strtolower ( $tag->slug ) ) ) / (strlen ( $phrase ) * $avg); if ($this->debug) echo '4--------
' . $tag . '-
$i=' . $i . '
$this->sorted [$i]=' . $this->sorted [$i] . '
' . $phrase . '4--------
'; $i ++; } } } // print_r($txt);if($this->debug) echo '|||||'; } if ($options == "title" and sizeof ( $tags ) > 0) { foreach ( $tags as $tag ) { // if($this->debug) echo '

tag:'. $tag.'

'; if (strlen ( $tag ) > 2) { $i = 0; // if($this->debug) echo '
-'.'
-'.'

tag:'. $tag.'

'; foreach ( $phrases as $phrase ) { $txt [$i] = $phrase; @$this->sorted [$i] += substr_count ( strtolower ( $phrase ), trim ( strtolower ( $tag ) ) ) / (strlen ( $phrase ) * $avg); if ($this->debug) echo '5--------
' . $tag . '
$i=' . $i . '
$this->sorted [$i]=' . $this->sorted [$i] . '
' . $phrase . '5--------
'; $i ++; } } } } // print_r($txt);if($this->debug) echo '|||||'; arsort ( $this->sorted, SORT_NUMERIC ); foreach ( $this->sorted as $key => $val ) { if ($this->debug) echo '

' . $key . "=" . $val . '


a=' . $a . '
numS=' . $this->options ["numSentences"] . '
' . $txt [$key] . '
'; $temp [$key] = $txt [$key] . "…"; } ksort ( $temp ); $text = ""; // print_r($temp); if (sizeof ( $temp ) > 0) { foreach ( $temp as $key => $val ) { $text .= $val; $txtLen = strlen ( preg_replace ( '/<[^>]*>/', " ", $text ) ); if ($txtLen >= $this->options ["maxLen"]) { $text = substr ( $text, 0, strrpos ( $text, " ", - ($txtLen - $this->options ["maxLen"]) ) ) . '…'; break; } } return $text; } else return false; } /** * enrich_excerpt() * * @param string $temp * @return string $temp * */ private function enrich_the_excerpt($temp) { if (strlen(trim($this->options ["more"])) > 1) $temp .= "\n" . apply_filters ( 'the_content_more_link', ''.($this->options ["more"]).'', null ) . "\n"; $temp = force_balance_tags ( $temp ); return trim ( $temp ) . " "; } } $Smart_excerpt = Smart_excerpt::get_instance (); /* * admin option */ if (is_admin ()) { // admin actions add_action ( 'admin_init', 'register_smaExc_settings' ); add_action ( 'admin_menu', 'smaExc_register_options_page' ); } function register_smaExc_settings() { register_setting ( 'smaExc-group', 'minLen' ); register_setting ( 'smaExc-group', 'maxLen' ); register_setting ( 'smaExc-group', 'useTitle' ); register_setting ( 'smaExc-group', 'useTags' ); register_setting ( 'smaExc-group', 'featured' ); register_setting ( 'smaExc-group', 'more' ); register_setting ( 'smaExc-group', 'numSentences' ); register_setting ( 'smaExc-group', 'keepHtmlTags' ); register_setting ( 'smaExc-group', 'stripAll' ); register_setting ( 'smaExc-group', 'stripTags' ); register_setting ( 'smaExc-group', 'skipManual' ); } /* * Registers the option page */ function smaExc_register_options_page() { add_options_page ( 'Smart Excerpt', 'Smart Excerpt', 'manage_options', 'smaExc_plugin', 'smaExc_page' ); } /* * Display the admin page */ function smaExc_page() { ?>

Excerpt length settings

Maximum number of sentences to display:
Maximum length in characters (the last word is not cut) characters
Do not analyse articles shorter than characters

Look and feel

Display featured image? />
Display "Read more"?
Which html tags do you want to remove?
*recommanded
/>all tags
/>*img />*br />*p />div />a href

Algorithm settings

Skip the manual excerpt? />
Use the words in the title (recommanded)? />
Use the article tags (if available)? />

Do you like this plugin?