Limiting excerpt and title length in wordpress

Sometimes you have a web page layout that will break if the content exceeds a certain size. For example, if the excerpt of a post is too long it could break your layout. Let’s see how we can apply a word limit on the excerpt and the title.

Limit The Excerpt:

The excerpt has a straightforward way. There is a WordPress filter called excerpt_length. To limit the words to 20 words, add the following code to your functions.php located in your theme folder /wp‑content/themes/[THEME]/functions.php.

function custom_excerpt_length( $length ) {
	return 20;
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Limit The Title:

The title is a bit trickier. There is a WordPress filter called the_title, but if we use this filter like we did with the excerpt, it will apply the limit in all of the website even on single post pages.

If we want to apply the limit on a certain page, we will have to find the location of the code responsible for displaying the title in this page. For example, in the Twenty Sixteen, the WordPress default theme, the post title code is located in the file wp‑content/themes/twentysixteen/template‑parts/content.php

When you locate the file, look for the_title() function. This function is responsible for displaying the title of the post. We need to modify the output of this function to apply our word limit

We can create a custom function to apply the limit but there is already a built-in WordPress function called wp_trim_words(). From the wordpress documentation:

This function is localized. For languages that count ‘words’ by the individual character (such as East Asian languages), the $num_words argument will apply to the number of individual characters.

Sounds good. Now let’s use it to wrap our title. First we need the title to be returned by the_title() not echoed directly which is the default. In order to do this we will pass the third argument as false. You only need to change the third argument, keep the first and the second argument as is:

the_title( '', '', false )

Now we will pass the returned title from the_title() to wp_trim_words():

echo wp_trim_words ( the_title ( '', '', false ), 4 , '...');

4: the number of words to keep
‘…’: is the characters that indicate there is more in the title. We can omit this argument here as this is the default.

Keep in mind the that any edits to theme files will be replaced if the theme is updated so it is better to create a child theme to keep your edits

WordPress header parser error

I had this weird problem when I uploaded my WordPress plugin RSS from aljazeera to the WordPress plugin directory. After installing the plugin it makes a notice that the plugin needs to be updated to version 0.0! and clicking update doesn’t seem to have any effect.

At the first glance I thought it is a problem in the readme.txt file in the SVN repository. WordPress provide a repository for plugins hosted on the WordPress plugin directory, but I checked it and every thing was right. Then I checked the plugin PHP files and every thing seemed OK. After hours of experimenting and googling I finally found the problem.

This was the header of the PHP file in the plugin:

 * Plugin Name: RSS from Aljazeera
 * Plugin URI :
 * Description: Display the latest Arabic RSS feed from news website
 * Version    : 1.2
 * Author     : Ahmed Essam
 * Author URI :
 * License    : GPL2
 * License URI:

My mistake was adding padding spaces to align all the “:” to be on the same column. The WordPress parser only accepts header values where the “:” is adjacent to the name. But you can put spaces after the “:”. I changed the header to be:

 * Plugin Name: RSS from Aljazeera
 * Plugin URI:
 * Description: Display the latest Arabic RSS feed from news website
 * Version:     1.2
 * Author:      Ahmed Essam
 * Author URI:
 * License:     GPL2
 * License URI:

Then every thing worked as it should.
Apparently you have to keep this format for every headers parsed by WordPress including theme headers.
Happy coding!