How To Automatically Control The Manual Excerpt in WordPress

WordPress Excerpt is an optional summary of your posts. In this tutorial, I will show you how to automatically control the Manually typed excerpt AND the Automatically generated excerpt at the same time, by using a simple function and without the need of a plugin.

The image below shows the result when using CODE-Y below. Notice that both the Manually typed Excerpt and the Auto-generated excerpt are present.

Boutros AbiChedid - Code (CODE-Y) that controls the Manually added Excerpt and the automatically generated Excerpt.

This is my Fifth tutorial related to the WordPress Excerpt. My 4 previous tutorials are:

The relationship between the Manual Excerpt, Automatic Excerpt, and Teaser is this: When a post has no manual excerpt added to the dashboard and the post template uses the_excerpt() tag, WordPress automatically generates an excerpt by selecting the first 55 words of the post followed by the unlinked ellipsis “[...]”. When the post template uses the_content() tag, WordPress will look for the More tag and create a teaser from the content that precedes the More tag. However, if a post has a manually added excerpt, then WordPress, by default, will display the content of the manual excerpt instead. However, in this case, the manually added excerpt will be returned untouched, with formatting if it exists. It will not be shortened.

Where To Add the Manually Typed Post Excerpt

If you don´t know where to add a manual excerpt, then read on …

To manually add an excerpt to a post, you need to login to your WordPress dashboard, find your post, and add the excerpt as shown in the picture below:

Manually adding an excerpt to a post in WordPress dashboard.

The Advantage of a Manual excerpt is that it is often used by search engines to describe search results. In this CASE, good excerpts increase Website traffic. Another major drawback of the auto-generatic excerpt is that the beginning of a post is usually an introduction and not a summary.

Auto-Generated Post Excerpt

In WordPress, if you do not provide a manually typed excerpt to a post, WordPress will display an automatically generated excerpt. By default, WordPress display the excerpt with the first 55 words of the post´s content, an un-linked ´[...]´ string at the end, and the text will be unformatted. This makes the excerpt one text paragraph without any line breaks.

CODE-Y Features

CODE-Y below has several features:

  1. No extra Plugin to your Blog. Caution about adding plugins to your theme.
  2. The code takes care of both scenarios: The manually added excerpt and the automatically generated excerpt. You can have both a manual excerpt on one post and and auto-generated excerpt on another post.
  3. Option to add a linked anchor to the end of the Excerpt.
  4. Option to add an un-linked Ellipsis ([…]) to the end of the Excerpt.
  5. Option to control the excerpt length.
  6. However; the code strips all HTML formatting in the excerpt.

Automatically Control the Manual Excerpt

To add CODE-Y: Open functions.php file located in your current theme´s folder and add (copy and paste) the following CODE-Y. Save the file and upload it to your server.

CODE-Y:
Note: To scroll within the code: You can also click on the code window and use your keyboard´s arrow keys.

<?php
/****************************CODE-Y***************************************
*  @Author: Boutros AbiChedid 
*  @Date: June 28, 2012
*  @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
*  @Description: Control the manually typed Excerpt AND the automatically 
*  generated Excerpt at the same time.
*  @Tested on: WordPress version 3.4.1
**************************************************************************/ 

function bac_manual_auto_excerpt($text) {
	global $post;
	$raw_excerpt = $text;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = strip_shortcodes( $text );
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]&gt;', $text);
	}	 
	$text = strip_tags($text);
	/*** Change the excerpt words length. If you like. ***/ 
	$excerpt_length = apply_filters('excerpt_length', 45);
	
	/*** Change the Excerpt ending. If you like. ***/
	$excerpt_end = ' <a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading.' . '</a>'; 
	$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
	
	$text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); 
	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 
}
add_filter('get_the_excerpt', 'bac_manual_auto_excerpt', 5);
?>	    

CODE-Y works for both the manually typed excerpt AND The automatically generated Excerpt. If there is a manual excerpt in the dashboard it will be displayed at the main page of the blog. If there is no manual excerpt added, then the auto-generated excerpt will be displayed at the main page of the blog.

For the Auto-generated excerpt: Watch out: If you are using the <!–more–> tag in your post content, make sure that it resides outside your defined excerpt length. If there is a More tag in your post, then the excerpt word count stops at either where the More tag is placed or it stops at your defined excerpt length whichever comes first.

For the Manually typed excerpt: Watch out: If you type a shorter excerpt in your dashboard, than the length defined in the code (line 22 of CODE-Y), then the excerpt displayed will not show the excerpt_end as defined in line 25 of CODE-Y.

CODE-Y works on WordPress 3.3 and higher. But I hope that you will always upgrade to the latest version.

CODE-Y does NOT preserve any HTML formatting in the Excerpt, be it manual or the auto-generated one.

The trick in this code is to use the wp_trim_words() function instead of wp_trim_excerpt().

How To Change the Excerpt Length in CODE-Y

CODE-Y is set at the Excerpt length of 45 words. If you like to change the Excerpt length, then find line 22 and replace it with the integer that you wish. Keep the length reasonable.

How To Replace the Excerpt More in CODE-Y

CODE-Y is set to add a ´Continue Reading´ link to the end of the Excerpt. Of course you can change the anchor to something else like ´Read More´. If you want WordPress to output an un-linked “[...]” at the end of the excerpt, which is the default, then replace Line 25 of CODE-Y with the following:

$excerpt_end = '[...]'; //This is the WP default.

CODE-Y Does Not Work for my Theme

If CODE-Y is not responding in your theme, make sure that the_excerpt() function exists in your theme´s index.php file. You can also check its existence in archive.php and search.php files.

How To Replace the_content() With the_excerpt()

Never show full posts in the main page. In your theme, If you have the_content() template tag, make sure to use the More tag in your post. They work together, otherwise your posts will be shown in full in the main page which can dramatically increase page loading time. One way to reduce page load time, is to replace the_content() template tag with the_excerpt() template tag.

To do that, open the index.php file located in your theme´s folder and replace the_content() with the_excerpt(). You could also do the same for archive.php and search.php files. (DO NOT TOUCH page.php or single.php files.)

//The '...' can be anything (or nothing) inside.
//Search and Replace the_content() template tag
<?php the_content('...'); ?>

//With the_excerpt() template tag
<?php the_excerpt('...'); ?>

NOTE: the_excerpt() template tag has no parameters. However, there is no harm from keeping them in case you wanted to revert back and use the_content() again. If you get any errors, then replace the_content(‘…’); with the_excerpt();.

To EMPHASIZE: the_content() template tag is used in conjunction with the More tag. Make sure to place the More tag somewhere at the beginning of the post. Otherwise, the whole content of the post will be displayed.

References:

Conclusion

In this tutorial, I showed you how to automatically control the Manually typed Excerpt AND also the Auto-generated excerpt at the same time, without the need of a plugin.

If you have any questions or anything else to say, please share your opinion in the comments section. Your opinion matters, unless it is a Spam.

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

5 Responses to “How To Automatically Control The Manual Excerpt in WordPress”

  1. Jessi Wohlwend says:

    Hi Boutros,

    I just read your articles on customizing the WP excerpt without a plugin, and I think they are really close to what I need, but not quite. I have the excerpts that show on my homepage set up the way that I want them, limited to 70 words, with a Read More link at the end and no img tags because a featured image thumbnail automatically displays because of my theme.

    And I would like to leave those automatic excerpts exactly the way they are, but I would like to be able to manually create an excerpt for each post as well and send that excerpt to my RSS feed. I want a longer excerpt that includes the first image in the post shown in my RSS feed, but when I’ve tried to change that, it also changes the excerpt that shows on my homepage or category, tag, and archive pages.

    Is there a way to leave automatic excerpts everywhere on the blog and send the manually generated excerpt to the RSS feed?

    - Jessi

  2. Mark :: Siloans says:

    Hello, please tell me how can I change text ‘continue reading’? When I write excerpt manually add_filter excerpt_more doesnt work…

  3. Jenny says:

    Hi Boutros

    hoping you can help….
    Saw your article on code you have for doing excerpts for blogs it was
    I have no clue about code…hence went above my head trying read it
    as to where about specifically to paste within the document and what
    actual entire code to paste

    We use Genesis and have lifestyle version 1.0 – its a static home page
    site with separate blog located on the menu.

    My issue is that of when people read the blog excerpts and not having
    any of the formatting applied in the main post stripped out and post
    which I have no idea how to prevent this happening, arghhh!

    Not sure if you have or know of a plug in (I know you advise
    against!) that might help me as doing code as I did is a little above my
    head BUT even willing to try pasting that in if it is fairly simple to
    know where specifically to place such a code and in what file it has
    to be in my genesis theme to do this in so I too can achieve the
    following:

    when people see the blog page and various excerpts for any posts, I
    want those blog post experts above the read more link they see to use
    same formatting applied in its actual main post and retain and not
    lose their formatting of bold, italic and several paragraphs showing,
    etc just as in this person who uses genesis has been able to do at
    : http://kendallsummerhawk.com/blog/
    and the audio mp3 player to also be seen like hers we use as well we
    can get a thumb nail image to show ok .

    Using the read more apply button in the editor does not even allow us
    to dictate how far to see stuff and simply applied where it wants
    within about 70 words each time.

    The Genesis frame work is up to date with latest version and the
    lifestyle theme 1.0 available widgets we have is featured post and
    page and featured left, right and bottom…..

    I tried a plugin called “Advanced excerpts” BUT, never made any
    difference others found hit and miss and the same not working for
    quite a few as well etc and it’s not been updated for two years!

    look forward to hearing from you.

  4. Ilan Kibrit says:

    Hi, VERY nice script… Works perfectly with 3.4.1!

    One question: Is there a way I can use the original manual excerpt, without trimming it?

    Thanks,
    Ilan