Simple WordPress Plugin to automatically add a quicktag

Posted on Posted in Blog

The <!–more–> quicktag is a very useful tool in WordPress but most WordPress users don’t even know it is there. It is very useful when you want your post to be presented as an “excerpt” (usually called teasers) in the front page, category or tag posts.

There are several ways to place this quick tag and the most common way is to use the “more” button of the WordPress editor in HTML mode. All you need to do is determine the excerpt of your post (usually the first paragraph); place the cursor at the end of the excerpt; and click the “more” button on the editor.

I’ve been using this method in other blogs and since I usually use the first paragraph as the excerpt, I decided to make a plugin on this one. It is a very simple plugin that uses “add_filter” function of WordPress and hook this to a single function. The role of the function is to determine whether the <!–more–> quicktag is already there and do corresponding action on the post.

Here is the plugin saved as add-more-quicktag.php:
[php]
quicktag after the first paragraph of each post. This quicktag acts as a delimiter to posts such that text before this will be shown in non single posts.
* Version: 1.0.0
*
* Author: Justin Villocido
* Author URI: http://www.veba-software.com/
*/

function add_more_quicktag ($post){

if ( preg_match(‘//’, $post)){
return $post;
} else{
$post = str_replace ( “rnrn”, “nn”, $post);
$posts = explode ( “nn”, $post);
$posts[0] = $posts[0] . ““; //the first paragraph
$post = implode( “nn”, $posts);
return $post;
}
}
add_filter ( ‘content_save_pre’, ‘add_more_quicktag’ );
[/php]

Let me explain some important things about the plugin. As I’ve said above, it uses add_filter which is used to filter the content right before saving it to the database. The keyword there is “content_save_pre” which instructs WordPress to filter the content using the add_more_quicktag function before saving it to database.

The add_more_quicktag function checks whether the content already contains the <!–more–> tag and if its there, the function doesn’t do anything. Otherwise, this function is going to add the quicktag at the end of the first paragraph.

So there you go. It is a very simple plugin that uses content filtering. You can probably add something to the plugin like set the description meta of the header to this excerpt by setting it as a postmeta.

Leave a Reply

Your email address will not be published. Required fields are marked *