Menu

RTCXpression

Close

WordPress Static Site Generator – Changes from within WordPress

- May 1, 2017

WordPress Static Site Generator When I wrote about installing WordPress locally, I recommended some plugins. One of them makes it easy to make changes before creating the static pages. I’m talking about “Code Snippets”. It’s what I use.

There’s a bunch of things you can remove from each page. There’s a bunch of things you can add. And then there’s a bunch of things you can change. A few code snippet functions will take care of all of it.

Remove Query Strings from your Static Assets

You can remove the query strings from the end of your CSS and JavaScript sources. This makes it easy to replace the assets in the source of each page with their CDN counterparts (with another function). This little function does the trick:

function remove_script_versions($src) {
  return remove_query_arg('ver', $src);
}
add_filter('script_loader_src', 'remove_script_versions', 100);
add_filter('style_loader_src', 'remove_script_versions', 100);

Remove Unnecessary Cruft from the Head Section

I’ve commented these one-line functions, just so you know what I’m doing:

remove_action('wp_head', 'feed_links', 2);               // Removes links to the general feeds: Post and Comment feeds
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

Add Lines to the Head Section

These lines should be edited and it really depends on what you’re doing. If you don’t use any of lines 2, 3, and 4, you can remove them. If you don’t use Google AdSense, you can remove the line for “googlesyndication”. DNS prefetching is supposed to speed things up. I listed what I prefetch. Other than the jQuery assets, your theme could be different.

function head_additions() {
  $output  = '<meta http-equiv="content-language" content="en-us">' . "n";
  $output .= '<meta name="google-site-verification" content="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">' . "n"; // Google Search Console
  $output .= '<meta name="msvalidate.01" content="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">' . "n";                       // Bing Webmaster Tools
  $output .= '<meta name="p:domain_verify" content="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">' . "n";                     // Pinterest
  $output .= '<link rel="dns-prefetch" href="https://ajax.googleapis.com">' . "n";            // jquery
  $output .= '<link rel="dns-prefetch" href="https://cdnjs.cloudflare.com">' . "n";           // underscore
  $output .= '<link rel="dns-prefetch" href="https://code.jquery.com">' . "n";                // jquery-migrate
  $output .= '<link rel="dns-prefetch" href="https://fonts.googleapis.com">' . "n";           // Google Fonts
  $output .= '<link rel="dns-prefetch" href="https://opensource.keycdn.com">' . "n";          // Font Awesome
  $output .= '<link rel="dns-prefetch" href="https://pagead2.googlesyndication.com">' . "n";  // Google AdSense
  echo $output;
}
add_action('wp_head','head_additions', 1);

Change Lines before they’re Displayed

The “foreach” loop is where the magic happens. The first three lines remove lines from the source. The next few lines replace a style sheet and some scripts with their online counterparts.

function ob_callback($buffer) {
  $temp_buffer = explode("n", $buffer);
  $buffer = '';
  foreach ($temp_buffer as $line) {
    if (strstr($line, '/comment-reply.min.js'))
      continue;
    if (strstr($line, '/wp-json/'))
      continue;
    if (strstr($line, '/xmlrpc.php'))
      continue;
    if (strstr($line, '/hueman/style.css'))
      $line = "<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,300italic,300,600&subset=latin,latin-ext' media='all'>";
    if (strstr($line, '/font-awesome.min.css'))
      $line = "<link rel='stylesheet' href='https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css' media='all'>";
    if (strstr($line, '/jquery.js'))
      $line = '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>';
    if (strstr($line, '/jquery-migrate.min'))
      $line = '<script src="https://code.jquery.com/jquery-migrate-1.4.1.min.js"></script>';
    if (strstr($line, '/underscore'))
      $line = '<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>';
    $line = str_replace('author/rtcunningham', '', $line);
    $buffer .= $line . "n";
  }
  return $buffer;
}
function ob_template_redirect() {
  ob_start('ob_callback');
}
add_action('template_redirect', 'ob_template_redirect');

The line with “hueman/style.css” is just what I do here. You can append the Google Font to another line in the source if you don’t have anything to replace. Just add a “n” and then the new line.

I leave only the domain name in my author links. The links point to the home page (which isn’t static). I could remove them altogether but I prefer not to.

While you’re working on this particular function set, you should be viewing the source of a post or page that would be affected. There’s a lot more you can do that what I’m showing you. Removing blank lines is probably a good idea – it can make each static file a little smaller.

Articles in this Series

This is a list of all the articles in this series. You should read each article in the order they’re presented. You could miss something important if you skip around.

Share this:

Categories: Technology

Tags: , , , ,

Previous and Next Articles (if any):

« »

More

You've made it this far down the page. Please read some of my more important pages if you have the time:

Comments Policy           Privacy Policy

RTCXpression established Feb 28, 2011
Copyright © 2013-2017 RT Cunningham
Hosted at Digital Ocean