Read More Patch

Update May 31st

I have completed an early version of this plugin. Feel free to download it! The administrative page is fairly bare but it does the job.

The information below is deprecated. Please review the patchset page for more information.

Update May 28th

I’ve gotten a plugin mostly written that accomplishes roughly what the patch here sets out. I haven’t finished the administrative section quite yet, but I expect to have something written by this weekend!


I was digging around the WordPress Codex today trying to figure out if there were a way to customize the output generated by the “teaser” options to the template function the_content(). Unfortunately, nothing is provided for supplying “before” and “after” wrappers as there exists with most other WordPress template functions. So, I’ve added this functionality myself. I suppose it would be better implemented as a WordPress plugin since it’s filter system is pretty powerful, but I honestly can’t be bothered learning their API just yet (give it time!). It does require a minor modification to the WP source as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
--- post-template.php.orig      2009-05-27 22:12:45.000000000 -0600
+++ post-template.php   2009-05-27 23:25:22.000000000 -0600
@@ -159,8 +159,10 @@
  * @param string $stripteaser Optional. Teaser content before the more text.
  * @param string $more_file Optional. Not used.
  */
-function the_content($more_link_text = null, $stripteaser = 0, $more_file = '') {
-       $content = get_the_content($more_link_text, $stripteaser, $more_file);
+function the_content($more_link_text = null, $stripteaser = 0, $more_file = '',
+                     $before = '', $after = '') {
+       $content = get_the_content($more_link_text, $stripteaser, $more_file,
+                                  $before, $after);
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
        echo $content;
@@ -176,7 +178,8 @@
  * @param string $more_file Optional. Not used.
  * @return string
  */
-function get_the_content($more_link_text = null, $stripteaser = 0, $more_file = '') {
+function get_the_content($more_link_text = null, $stripteaser = 0, $more_file = '',
+                         $before = '', $after = '') {
        global $id, $post, $more, $page, $pages, $multipage, $preview, $pagenow;
 
        if ( null === $more_link_text )
@@ -216,9 +219,13 @@
                if ( $more ) {
                        $output .= '<span id="more-'.$id.'"></span>'.$content[1];
                } else {
+                   if ( ! empty ($before) )
+                       $output .= $before;
                        $output = balanceTags($output);
                        if ( ! empty($more_link_text) )
                                $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
+                   if ( ! empty ($after) )
+                       $output .= $after;
                }
 
        }

The patch adds two new options to the_content() so the function definition now appears as:

the_content([$text = null[, $teaser[, $file[, $before[, $after]]]]])

This allows you to wrap the output of $text in <div> blocks like so:

1
2
3
4
5
<?php
 
the_content('Read more...', false, null, '<div class="read-more-link">', '</div>');
 
?>

Copy the patch to your $WORDPRESS/wp-includes directory and apply it:

$ patch -p0 <post-template.php.patch

Since this does require a patch to the WordPress sources (a small one, admittedly), I’d probably suggest holding off on using it until I write a plugin. I’m sure there’s one that already has this functionality. :)

***

Leave a comment

Valid tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>