Skip to content

WordPress: how to generate DocBook style posts

I’m regular follower of http://www.vogella.de site. What I liked most about the site is its presentation and a way to show users how up to date the articles are. Users like to know how fresh the content is and how frequently they are updated. I would also don’t like to read the technical articles which are pretty old and may not be up to how technology is changing.

The site uses “DocBook XSL Stylesheets” tool to generate the contents and manage the revisions. As we all know that most of the blogging sites are based on WordPress. I have done my homework on how easy it is to convert the wordpress posts into revision based article.

sample screenshot:

Here is the code base which you can try out.

Note that all these code changes have to happen in your theme code.

1. functions.php

add below function to functions.php file.


<?php

function get_post_revision_info($postId) {
 global $wpdb;

 $sql = "("
 . " SELECT ID, post_author, post_name, post_modified, post_parent, post_type"
 . " FROM wp_posts"
 . " WHERE post_parent =".$postId.""
 . " AND post_type = \"revision\""
 . " )"
 . " UNION"
 . " ("
 . " SELECT ID, post_author, post_name, post_modified, post_parent, post_type"
 . " FROM wp_posts"
 . " WHERE id =".$postId.""
 . " AND post_type = \"post\""
 . " ) ORDER BY `ID` ASC";

$posts=$wpdb->get_results($sql);
 $header="";
 $lastrevision = "1";
 $output="";
 $postYear = date('Y'); ;

 if ($posts)
 {
 $cnt = 1;

 $output .= "<h3>Revision History</h3>";
 $output .="<table>";

 foreach ($posts as $post)
 {
 $lastrevision = $cnt;

 $author = get_the_author_meta( 'display_name', $post->post_author);
 $datetime = strtotime($post->post_modified);
 $modified = date( 'm.d.Y', $datetime);

 $rev = "Revision ".$cnt.".0";
 $updated = "updated";
 if($post->post_type == "post")
 {
 $updated = "created";
 $postYear = date( 'Y', $post->post_modified ); //post created date
 }

 $output .="<tr>";
 $output .="<td>".$rev."<td>"."<td>&nbsp;</td>";
 $output .="<td>".$updated."<td>"."<td>&nbsp;</td>";
 $output .="<td>".$modified."<td>"."<td>&nbsp;</td>";
 $output .="<td>".$author."<td>"."<td>&nbsp;</td>";
 $output .="</tr>";

 $cnt++;
 }
 $output .="</table>";
 }
 else
 {
 $output .= "no revision history found.<br />";
 $output = $sql;
 }

 $curYear = date('Y'); ;
 $copyright = "Copyright &copy; ".$postYear . (($postYear != $curYear) ? '-' . $curYear : ''). " ".get_the_author();

 $header .= "Author ".get_the_author()."<br>";
 $header .= "Current Version ".$lastrevision.".0"."<br>"; //1.0, 2.0...
 $header .= $copyright."<br>";
 $header .= date("m.d.Y")."<br>";

 $footer = "<h3>Excerpts:</h3>";
 $footer .= "<i>".get_the_excerpt()."</i><br/><br/>";
 $footer .= "<h2>Article Details</h2>";

 return "<h2>".$header."</h2><br/>".$output."<br/><br/>".$footer."<br/>";
}

2. single.php

add below code (a call to post_revision_info) in single.php


<div class="entry-content">
<?php print get_post_revision_info(get_the_ID()); ?>

This is my first version of the code. it may not generate the well-formed HTML code or may not suit to your theme.
so please change the code according to your need.

your comments will encourage me to come up with more articles on the related topic.



2 Comments

  1. HughP wrote:

    It seem that this code should be wrapped into a plugin… rather than a theme implementation.

    Tuesday, October 22, 2013 at 8:53 pm | Permalink
  2. Praveendra Singh wrote:

    Good idea HUGHP.
    I don’t have expertise in plugin development. Are you interested in creating the plugin? If yes, please go ahead.

    Friday, March 7, 2014 at 9:11 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*