Providing Shims for functions introduced in or after WordPress 5.0

Sometimes you need to use Themes or Plugins which rely on Functions present only in WordPress 5.0 or above.

For example this can be the case when you are migrating a Site from WordPress to ClassicPress and need time to adapt to the new CMS.
Perhaps you are using a Plugin or Theme that works just fine with ClassicPress but throws an error just because of one or a few functions not present in ClassicPress.

In those cases, you can workaround the issue by providing so called “Shims” for the missing functions.

In this Guide we will show some of the most common missing functions and how to add a Shim for them.

How to add Shims to your websiteLink to this section

A Shim is in short, a function that acts as a Placeholder for a missing piece of code. You can generally craft Shims by wrapping the function declaration in a ! function_exists( 'function_name' ) wrapper.

Inside this conditional statement you would then declare the missing function and provide either the real function’s code, or a minimal version thereof to grant smooth functioning of the program.

You can add those Shims either to a Custom Plugin, a Must Use Plugin or your current Theme’s functions.php file. Of course there are many other ways to add Custom Code to a ClassicPress install, but those are the most common.

Add a Shim for wp_body_openLink to this section

The wp_body_open was added in WordPress 5.2 and is used by Themes to add a hook just after the HTML Body tag has opened. In general, Themes do already provide a Shim for this function, however, there are cases where it is missing. If you run such a Theme on a ClassicPress install it will fail in an error. It is planned to add the wp_body_open function in ClassicPress 1.3.0, but until then, you need a Shim if your theme has none yet.

The code for this Shim looks like this:


if ( ! function_exists( 'wp_body_open' ) ) {
        function wp_body_open() {
                do_action( 'wp_body_open' );
        }
}

Add a Shim for do_blocksLink to this section

The do_blocks function was added in WordPres 5.0 and Plugins (or Themes) use it like the “new” do_shortcode. Unfortunately, it is very common amongst such Plugins and Themes to not check if the function exists, and running such Theme or Plugin on a ClassicPress install will result in Fatal errors. The problem with do_blocks is, we cannot provide a fully functional Shim for it, since it calls a whole lot of other Functions that all where introduced after WordPress 4.9, and thus.

Also, the function is as the name suggests intended for Blocks, which ClassicPress does not ship at all, for obvious reasons.

All we can do here, is provide a Shim that skips the entire Blocks logic. Running a Theme or Plugin that relies on do_blocks and providing a Shim for it on ClassicPress will in most cases still show the content on the Front End, but sometimes, when Custom Blocks (provided by said Themes or Plugins) where used on the site, then those Blocks will logically not work anymore, even if we provide a Shim.

In most cases, the content will just be returned to the browser as-is, this means you may or may not see valid output in the front end. But at least, providing a Shim will not result in Fatal Errors and gives you time to change your content back to HTML or other, supported output methods (like ShortCodes, or Widgets).

The minimal Shim for a do_blocks call looks like so:


if ( ! function_exists( 'do_blocks' ) ) { 
  function do_blocks( $content ) {
    return $content;
  }
}

This effectively returns the Content that in WordPress would be passed thru the Blocks logic, directly back to the program, and hence it will be output as it is entered in the Editor.