How to check PHP version before switching theme

We recently started created a new theme for our company website. Obviously we were excited about using all the swish (relatively) new PHP features, closures, short array syntax etc etc. As you may or may not know WordPress’ minimum PHP version is PHP 5.2.4 which has none of those features and will error if it finds them. This isn’t really a problem for us as it’s a private theme and we have full control of the server, however I have seen these PHP features used in themes before and obviously it causes some confusion. So here is how to check the PHP version when switching theme.

 

// Minimum required version
define( 'THEME_REQUIRED_PHP_VERSION', '5.3.0' );

add_action( 'after_switch_theme', 'oz_check_theme_setup' );
function oz_check_theme_setup(){

	// Compare versions
	if ( version_compare( PHP_VERSION, THEME_REQUIRED_PHP_VERSION, '<' ) ) :

		// Theme not activated info message
		add_action( 'admin_notices', 'oz_php_version_admin_notice' );
		function oz_php_version_admin_notice() {
			?>
			<div class="update-nag">
				<?php printf( __( 'This theme requires a minimum PHP version of %s. Your version is s%. Your previous theme has been restored.', 'text-domain' ), THEME_REQUIRED_PHP_VERSION, PHP_VERSION ); ?>
			</div>
			<?php
		}

		// Switch back to previous theme
		switch_theme( get_option( 'theme_switched' ) );
			return false;

	endif;
}

WordPress 4.3 – Notes and release date

WordPress 4.3 is due on Tuesday, 18th August. Here is a brief run down of all the coming changes, enhancements and bug fixes.

You can find the official release notes here. And download the pre-release candidate to test your themes and plugins here.

Please, please, please download it and test your plugins, it only takes a few minutes and will potentially save a load of heartache for other people.

How to change the default WordPress user admin color theme

As part of an effort to clean up the WordPress admin area for our authors at work we started removing elements from the dashboard they don’t need or want in an effort to make it less distracting for them. As well as removing text and unnecessary menus we also decided to change from the default dark grey theme to a light grey which we thought would fit better with our branding.

Force users to use a certain admin color theme

Add this to your theme’s functions.php. For every user it will force them to use the light theme.

/**
 * Force contributors to use the light colour scheme
 *
 * @param  string $result
 * @return string
 */
function oz_change_admin_color( $result ) {
	return 'light';
}
add_filter( 'get_user_option_admin_color', 'oz_change_admin_color' );

The complete list of default WordPress admin color themes is: default, light, blue, coffee, ectoplasm, midnight, ocean, sunrise.

How to the remove “Screen Options” and “Help” tabs from the wp-admin

Ok so part 2 in my loosely tied together series on “Cleaning up the WordPress admin” is about how to remove the “Screen Options” and “Help” tab from the top right of the screen. In case you missed part 1 – cleaning up the admin footer in my mini series, the reason I’m doing this is to help clean up the admin to give the writers on our site a much cleaner / easier / less distracting writing experience. As usual, add the following to your theme’s functions.php file.

 

Removing the Help Tab

Unsurprisingly it’s pretty easy to do with a filter. You can of course wrap it in a if statement if you want to conditionally remove it.


/**
* Removes the Help tab in the WP Admin
*
* @param array $old_help
* @param int $screen_id
* @param obj $screen
* @return array
*/
function oz_remove_help_tabs( $old_help, $screen_id, $screen ){
	$screen->remove_help_tabs();
	return $old_help;
}
add_filter( 'contextual_help', 'oz_remove_help_tabs', 999, 3 );

Removing the Screen Options Tab

This is actually even easier:

add_filter( 'screen_options_show_screen', '__return_false' );

Of course if you you want to conditionally show it you’ll need to point it to a custom function rather than the WordPress __return_false one. Although simple the only slight snag with this method is that you’ll lose any previously customised screen options, (columns on the admin homepage for example). However an anonymous user in this excellent stackoverflow question provides a solution.

function oz_remove_screen_options( $display_boolean, $wp_screen_object ){
	$blacklist = array('post.php', 'post-new.php', 'index.php', 'edit.php');
	if ( in_array( $GLOBALS['pagenow'], $blacklist ) ) {
		$wp_screen_object->render_screen_layout();
		$wp_screen_object->render_per_page_options();
		return false;
    }
	return true;
}
add_filter( 'screen_options_show_screen', 'oz_remove_screen_options', 10, 2 );

Whilst very similar this will not lose any saved screen options and will only remove the tab for the pages listed in the $blacklist array.

Part 3 is cleaning up the WordPress Dashboard

How to remove the text in the wp-admin footer

After a brief chat with our UI/UX chap we decided it would make sense to remove the text at the bottom of the WordPress admin. “Thank you for creating with WordPress” on the bottom left, and the WordPress version on the bottom right. I agree it’s hardly offensive but we decided it was a bit of a distraction for our writers. We have a lot of them and they all use the wp-admin for writing articles so we’re trying hard to make the process easier and the dashboard cleaner. Whilst the text in the footer isn’t that promient we felt it’d help with the “clean look” we’re tying for. So I set to work.

Bottom left text

The bottom left text it really easy, add the following to your themes functions.php:

add_filter( 'admin_footer_text', '__return_false' );

This will completely remove it, if you wanted to keep it but alter the text:

/**
 * Alters the bottom left admin text
 *
 * @return string
 */
function oz_alter_wp_admin_bottom_left_text( $text ) {
	return sprintf( __( 'Ear more <a href="%s" title="toast" target="_blank">toast</a>' ), 'https://www.google.com/search?q=toast' );
}
add_filter( 'admin_footer_text', 'oz_alter_wp_admin_bottom_left_text' );

Bottom right text

Ok so what about the bottom right. Slightly trickier as we need to remove the filter:

/**
 * Hide the WP version in the footer
 *
 * @return null
 */
function oz_admin_dashboard_footer_right() {
	remove_filter( 'update_footer', 'core_update_footer' );
}
add_action( 'admin_menu', 'oz_admin_dashboard_footer_right' );

Of course, if you just want to show the version for the administrator, (as we’re doing) you can wrap the remove_filter in an if statement:

/**
 * Hide the WP version in the footer for everyone but administrators
 *
 * @return null
 */
function oz_admin_dashboard_footer_right() {
	if ( ! current_user_can( 'update_core' ) ) {
		remove_filter( 'update_footer', 'core_update_footer' );
	}
}
add_action( 'admin_menu', 'oz_admin_dashboard_footer_right' );

So there you go. Much cleaner! In the same vein, how to remove the “Help” and “Screen Options” tabs is up next.