Require a certain core version as a minimum requirement in the WordPress theme

In this post I explain how you as a developer can set a certain core version as a minimum requirement and requirement in your WordPress theme. We simply take a look at the technical implementation in the new standard TwentySeventeen theme.

As a developer, I would like to be able to use the latest features and functions of the core in my WordPress themes. Pretty much every major release in recent years has introduced useful template tags, hooks or completely new functionality such as logos and starter content for theme developers.

Downward compatible use of new functions

The first option is to use additional queries and function_exists wrappers to check whether the desired functionality is available.

 // Check if WordPress Core Logo is available if ( function_exists( 'the_custom_logo' ) ) { the_custom_logo(); } else { // Display old logo option for WordPress 4.4 and earlier }

With the code snippet, the theme logo of the core is displayed, which is available from WordPress 4.5. For earlier versions, for example, a logo can be displayed that was uploaded with a customizer option of the theme.

As a developer, I can use the new function the_custom_logo () of the core , and at the same time keep my theme compatible with older WordPress versions.

The major disadvantage of this method is that additional fallback functions are required and the code is unnecessarily bloated. In addition, WordPress is designed in such a way that users should actually always use the latest version. There is no good reason to use an outdated core version.

Define a certain core version as a minimum requirement

The second option is therefore much more elegant, with which a certain WordPress core version is required as a minimum requirement for the WordPress theme.

All default themes (TwentyFifteen, TwentySixteen, etc.) use this method.

To illustrate this, I would like to take a look at the new standard theme TwentySeventeen. The theme requires the use of WordPress 4.7 and cannot be used with earlier versions of WordPress.

This is technically implemented with the following instruction:

 /** * Twenty Seventeen only works in WordPress 4.7 or later. */ if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '<' ) ) { require get_template_directory() . '/inc/back-compat.php'; return; }

This is the very beginning in the f unctions.php the theme.

With $GLOBALS['wp_version'] we get the current version of the core. With the PHP function version_compare this is compared with the required version of 4.7-alpha. If the WordPress version is too low, the file /inc/back-compat.php is loaded and the further execution of the functions.php of the theme is canceled with return .

The file that has now been loaded contains three functions that prevent the installation and execution of the theme in earlier WordPress versions.

Function 1 blocks the activation of the theme and switches back to the standard theme of the core version used.

 /** * Prevent switching to Twenty Seventeen on old versions of WordPress. * * Switches to the default theme. * * @since Twenty Seventeen 1.0 */ function twentyseventeen_switch_theme() { switch_theme( WP_DEFAULT_THEME ); unset( $_GET['activated'] ); add_action( 'admin_notices', 'twentyseventeen_upgrade_notice' ); } add_action( 'after_switch_theme', 'twentyseventeen_switch_theme' );

The other two functions prevent the customizer and the theme preview from being executed, in which a theme would also be executed although it is not activated.

The user receives the same error message for all three functions:

 sprintf( __( 'Twenty Seventeen requires at least WordPress version 4.7. You are running version %s. Please upgrade and try again.', 'twentyseventeen' ), $GLOBALS['wp_version'] );

You can study the complete file in the theme's Github Repo .

Conclusion

With the requirement of a minimum core version for the WordPress theme, developers can use the latest core features without having to include every new function with additional function_exists queries.

In addition, PHP errors are avoided if users want to install the theme with earlier versions. Instead, a message is displayed asking for a core update.