RT Cunningham

WordPress Static Site Generator, Part 5 – Functions

After the functions we need for the WordPress static site generator, the actual generation script will be all that’s left.

If you’re not comfortable with editing files, you may be better off using a plugin for your functions. It has the advantage of retaining the functions when you switch themes. In the following function snippets, I’m going to leave out the opening and closing PHP tags. If you use a functions.php file, you need to add them yourself.

WordPress Static Site Generator – Functions to Remove Unnecessary Lines

The first snippet isn’t a function. It calls existing functions. Unfortunately, this won’t remove everything we need to remove.

remove_action('wp_head', 'feed_links', 2);               # Removes links to the general feeds (Posts and Comments)
remove_action('wp_head', 'feed_links_extra', 3);         # Removes the links to the extra feeds such as category feeds
remove_action('wp_head', 'wlwmanifest_link');            # Removes the link to the Windows Live Writer manifest file
remove_action('wp_head', 'wp_generator');                # This isn't a normal WordPress installation
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); # This is a link that no longer works

These functions use output buffering to change lines just before they’re displayed. It shouldn’t be too hard to figure out. You’ll understand the “feed” line when we get to the next article. As you can probably see, this routine can be used to change lines as well as skip them.

add_action('template_redirect', 'ob_template_redirect', 99);
function ob_template_redirect() {
function ob_callback($buffer) {
  $temp_buffer = explode("\n", $buffer);
  $buffer = '';
  $skip = 0;
  foreach ($temp_buffer as $line) {
    if (stristr($line, '<pre')  || stristr($line, '<code'))  $skip = 1;
    if (stristr($line, '</pre') || stristr($line, '</code')) $skip = 0;
    if (!$skip) {
      $line = trim($line);
      if ($line == '') continue;
      if (strstr($line, '/s.w.org')) continue;
      if (strstr($line, '/wp-json/')) continue;
      if (strstr($line, '/xmlrpc.php')) continue;
      if (strstr($line, 'Schema plugin')) continue;
      if (strstr($line, 'Yoast SEO plugin')) continue;
      if (strstr($line, '<link rel="next"')) continue;
      if (strstr($line, '<link rel="prev')) continue;
      if (is_feed()) $line = str_replace('/feed', '/feed.rss', $line);
    $buffer .= $line . "\n";
  return $buffer;

WordPress Static Site Generator – Functions to Change the System

First, we need to change the taxonomy forward slashes to dashes. A forward slash points to a subdirectory in a file system (pretty permalinks won’t work on static sites). I’m not supporting post tags and you’ll understand why when I’m through with all this.

add_action('init', 'change_taxonomy_permalinks', 1);
function change_taxonomy_permalinks() {
  global $wp_rewrite;
  $wp_rewrite->author_structure = '/' . $wp_rewrite->author_base . '-%author%';
  add_permastruct('category', 'category-%category%');

Next, we need to make page URIs have priority over taxonomy URIs. I found the code here and shortened it a bit.

add_action('init', 'ppot_init');
function ppot_init() {
  $GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
add_filter('page_rewrite_rules', 'ppot_collect_page_rewrite_rules');
function ppot_collect_page_rewrite_rules($page_rewrite_rules) {
  $GLOBALS['ppot_page_rewrite_rules'] = $page_rewrite_rules;
  return array();
add_filter('rewrite_rules_array', 'ppot_prepend_page_rewrite_rules' );
function ppot_prepend_page_rewrite_rules($rewrite_rules) {
  return $GLOBALS['ppot_page_rewrite_rules'] + $rewrite_rules;


This shortcode will provide a list of all posts (or articles) in reverse chronological order with the date. Use [article-list] on a page.

add_shortcode('article-list', 'shortcode_article_list');
function shortcode_article_list() {
  $posts_array = get_posts(array('posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish'));
  $output = '<ul>';
  foreach ($posts_array as $post) {
    $a = explode(' ', $post->post_date);
    $output .= '<li><a href="' . home_url('/') . $post->post_name . '">' . wptexturize($post->post_title) . '</a> (' . $a[0] . ')</li>';
  $output .= '</ul>';
  return $output;

This shortcode will provide a list of all the categories in alphabetical order. Use [category-list] on a page.

add_shortcode('category-list', 'shortcode_category_list');
function shortcode_category_list() {
  $posts_array = get_terms(array('taxonomy' => 'category', 'orderby' => 'name', 'hide_empty' => true));
  $output = '<ul>';
  foreach ($posts_array as $cat) {
    $output .= '<li><a href="' . home_url('/') . 'category-' . $cat->slug . '">' . wptexturize($cat->name) . '</a></li>';
  $output .= '</ul>';
  return $output;

This shortcode will provide a list of all the posts belonging to a single category. It’s a replacement for archive pages.

add_shortcode('category-page', 'shortcode_category_page');
function shortcode_category_page() {
  $slug = str_replace('category-', '', $_SERVER['REQUEST_URI']);
  $posts_array = get_posts(array('category_name' => $slug, 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish'));
  $output =  "<ul>\n";
  foreach ($posts_array as $posts) {
    $a = explode(' ', $posts->post_date);
    $output .= '<li><a href="' . home_url('/') . $posts->post_name . '">' . wptexturize($posts->post_title) . '</a> (' . $a[0] . ')</li>' . "\n";
  $output .= "</ul>\n";
  return $output;

You need to create a page to list all the categories. Every time you add a category and assign it to a post, you have to create another page with the category slug and title. The easiest way to do it is copy the slug and title from the category list. It may seem like a lot of work but it really isn’t. Most people don’t add new categories every day.

Post tags, on the other hand, would eat up your time. You’d have to create a lot of matching pages. Don’t bother if you value your sanity.

April 22, 2018
Web Development

You May Also Like: