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 ["isActive"] = get_option ( 'isActive', FALSE ); $this->options ["admin"] = get_option ( 'admin', FALSE ); $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 ["featuredSize"] = get_option ( 'featuredSize', 'thumbnail' ); $this->options ["stripAll"] = get_option ( 'stripAll', true ); $this->options ["stripTags"] = get_option ( 'stripTags', array ( "img", "br", "p" ) ); $this->options ["custTags"] = get_option ( 'custTags', '' ); $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 ); $this->options ["alignFeat"] = get_option ( 'alignFeat', 'alignleft' ); } /** * 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"] or (! $this->options ["useTitle"] and ! $this->options ["useTags"]) or ! $this->options ["isActive"] or ($this->options ["admin"] and !is_admin ())) { // Short article limit if ($this->options ["featured"] and has_post_thumbnail ()) echo get_the_post_thumbnail ( null, $this->options ["featuredSize"], array ( 'class' => $this->options ["alignFeat"] ) ); $pos = @strrpos ( $content, " ", - ($content - $this->options ["maxLen"]) ); $content = substr ( $content, 0, ($pos ? $pos : $this->options ["maxLen"]) ) . '…'; 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 && $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"] && sizeof ( $phrases ) > 0) { $temp = $this->build_the_excerpt ( $tags, $phrases, "tags", $avg ); } // Personal tags $tags = explode ( ",", $this->options ["custTags"] ); if (sizeof ( $tags ) > 0 && $tags [0] != '' && sizeof ( $phrases ) > 0) { $temp = $this->build_the_excerpt ( $tags, $phrases, "cust", $avg ); } if ($temp) { if ($this->options ["featured"] and has_post_thumbnail ()) echo get_the_post_thumbnail ( null, $this->options ["featuredSize"], array ( 'class' => $this->options ["alignFeat"] ) ); echo $this->enrich_the_excerpt ( $temp ); return null; } else { // Short article if ($this->options ["featured"] and has_post_thumbnail ()) echo get_the_post_thumbnail ( null, $this->options ["featuredSize"], array ( 'class' => $this->options ["alignFeat"] ) ); echo $this->strip_the_tags ( 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 '|||||'; if ($options == "cust" 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] = (isset ( $txt [$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) { $temp = force_balance_tags ( $temp ); if (strlen ( trim ( $this->options ["more"] ) ) > 1) $temp .= "\n" . apply_filters ( 'the_content_more_link', '' . ($this->options ["more"]) . '', null ) . "\n"; 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', 'admin' ); register_setting ( 'smaExc-group', 'isActive' ); 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', 'featuredSize' ); 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', 'custTags' ); register_setting ( 'smaExc-group', 'skipManual' ); register_setting ( 'smaExc-group', 'alignFeat' ); } /* * 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() { ?>

A Smart Excerpt

/>active
/>only for admin

Excerpt length settings

Maximum number of sentences to display:
Set this setting to a value proportional the desired length of the excerpt (about 100 characters per sentence)
Maximum length in characters:
Set the excerpt length limit in characters (the last word is not cut in the middle)
characters
Articles minimum length to consider
Set the length under which you don't want to apply any analysis
characters

Look and feel

Featured image?
Display the featured image of your article (if available)
/>
Featured size?
Select the size of the picture to display
0 or get_option ( 'featuredSize' ) == '') echo 'checked="checked"'; ?> />thumbnail 0 ) echo 'checked="checked"'; ?> />medium 0 ) echo 'checked="checked"'; ?> />large 0 ) echo 'checked="checked"'; ?> />full
Align left or right?
The featured picture will get align the desired side, with the excerpt written on the side of the image
0 or get_option ( 'alignFeat' ) == '') echo 'checked="checked"'; ?> />left 0 ) echo 'checked="checked"'; ?> />right
Display "Read more"?
If you want to add the link/button, simply write it in the textbox. Leave the box empty if you don't want to show anything
Which html tags do you want to remove?
Choose which html tag you want to strip of the generated text. It is recommanded to strip the tags having a *
/>all tags
/>*img />*br />*p />div />a href

Algorithm settings


Uncheck the following two checkboxes for making the plugin function without analysing the content
Skip the manual excerpt?
If you want to avoid displaying the excerpt written manually in your post, unflag this checkbox
/>
Use the words in the title (recommanded)?
This requests the plugin to evaluate the article considering similarities with the title of the article
/>
Use the article tags (if available)?
This requests the plugin to evaluate the article considering the tags attached to the article
/>
Any personal keyword to prioritize?
Write in this box any word which is particularly relevant to your website and for which you want to increase the priority.
Enter the words separated by a comma, or leave it blank if you don't to use this feature.

Do you like this plugin?