Setting different static WordPress home pages for different themes

You can get WordPress to use a different theme to different visitors to your site. For example, serving up a mobile theme for visitors using a phone. Well, perhaps you want to alter some of the WordPress settings too, like displaying an alternative homepage.

Here’s a bit of code that shows either your blog posts, or a specific static home page depending on which theme is active.

global $static_theme_name;
$static_theme_name = 'Twenty Twelve';
add_filter( 'pre_option_show_on_front', 'tcb_pre_option_show_on_front' );
function tcb_pre_option_show_on_front( $false ){
  global $static_theme_name;
  $theme = wp_get_theme();
  if( $theme->name == $static_theme_name ):
    return 'page';
  endif;

  return 'posts';
}

add_filter( 'pre_option_page_on_front', 'tcb_pre_option_page_on_front' );
function tcb_pre_option_page_on_front( $false ){
  global $static_theme_name;
  $theme = wp_get_theme();
  if( $theme->name == $static_theme_name ):
    return 30;
  endif;

  return false;
}

This bit of code either needs to go in a plugin, or it can be put in both theme’s functions.php. Change the theme name as required.

WordPress – how to get the slug of your post or page?

Finding out the slug for a page or post is one of the most popular search terms on this blog. I find it a little bizarre that my little function generates so much interest, but that’s just one of the unexpected joys of blogging.

I have decided to follow-up my first post on getting the post slug, taking in to consideration the comments and main use of the function. Hopefully, though a small change, it is an improvement.

I’ve split it into two functions. The first forming the base for the second.

Function: get_the_slug()

/**
 * The the post or page slug from post ID
 */
function get_the_slug( $id=null ){
  if( empty($id) ):
    global $post;
    if( empty($post) )
      return ''; // No global $post var available.
    $id = $post->ID;
  endif;

  $slug = basename( get_permalink($id) );
  return $slug;
}

WordPress appears to be adopting the style of having get_ and the_ function prefixes to get or display content. So here is the complimentary function. I’ve removed some hooks that I don’t think anyone used, instead just applying one (well named) filter.

Function: the_slug()

/**
 * Display the page or post slug
 *
 * Uses get_the_slug() and applies 'the_slug' filter.
 */
function the_slug( $id=null ){
  echo apply_filters( 'the_slug', get_the_slug($id) );
}

Example use of the_slug() and get_the_slug() functions:

<div id="some-post-container-<?php echo get_the_slug(); ?>">
  The post slug is <strong><?php the_slug(); ?></strong>.
  Lorem whatsit here
</div>

Theme (or Plugin) Development – Attach hook to version change (flush rewrite rules)

The rewrite rules for WordPress control which pages and posts to display based on the URL. These are normally updated via the Permalinks setting. When you hit ‘save changes’  all the rules for which URL maps to which page are saved to the database.

When developing plugins and themes it is common to add some custom post types, or custom taxonomies. These both add to the rewrite rules. Rather than constantly clicking to save the permalinks button, we can use the flush_rewrite_rules() function call. It is expensive to do (calculating the rules and writing to the database uses lots of server resources). However, you don’t want to make your end-user have update their permalinks manually every time you release an update. This is why the codex recommends hooking into the activation hook, as this will get fired when the plugin is installed or updated from wordpress.org.

I am often faced with two scenarios that are not covered by the activation hook. Firstly, whilst developing I don’t update my plugin like that – I just change the code. And secondly I mostly write bespoke code for clients not hosted on wordpress.org, and updated using version control, not from inside WordPress admin.

So I need some other trigger. Something that happens automatically, but not all the time. I decided to hook into the theme version changing.

add_action( 'admin_init', 'tcb_version_update_check' );
function tcb_version_update_check(){
  $theme           = wp_get_theme();
  $in_file_version = $theme->Version;
  $slug            = $theme->stylesheet;
  $optionkey       = "tcb_vesion_check_{$slug}";
  $in_db_version   = get_option( $optionkey, 0 );

  $version_diff    = version_compare( $in_db_version, $in_file_version );
  if( !$version_diff ) return;

  error_log( "Theme version has changed: $in_db_version -> $in_file_version" );
  if( $version_diff == 1 ) :
    error_log( "Theme version has gone down. Doesn't compute. Not running update hook." );
    return;
  endif;

  do_action( 'tcb_theme_version_update', $in_file_version, $in_db_version );

  update_option( $optionkey, $in_file_version );
}

add_action( 'tcb_theme_version_update', 'tcb_update_flush_rewrite_rules', 10, 2 );
function tcb_update_flush_rewrite_rules( $new_version, $old_version ){
  error_log( "Flushing rewrite rules" );
  flush_rewrite_rules( false );
}

What I’ve done is hooked into admin_init to check whether the theme version has changed. Added a little sanity check to see if it has gone down!?! And invoked my own hook to let myself expand on the version change trigger.

How fast is your WordPress? Show Speed and MySQL query count in footer

Ever wondered how fast your website is? Well, with WordPress it is very easy to find out:

  • how long your page spent in PHP processing core and theme code
  • how many mysql queries have run during the processing

Paste this into your functions.php file:

add_action( 'wp_footer', 'tcb_note_server_side_page_speed' );
function tcb_note_server_side_page_speed() {
  date_default_timezone_set( get_option( 'timezone_string' ) );
  $content  = '[ ' . date( 'Y-m-d H:i:s T' ) . ' ] ';
  $content .= 'Page created in ';
  $content .= timer_stop( $display = 0, $precision = 2 );
  $content .= ' seconds from ';
  $content .= get_num_queries();
  $content .= ' queries';
  if( ! current_user_can( 'administrator' ) ) $content = "<!-- $content -->";
  echo $content;
}

This will show the page speed in the footer for the administrator. It will hide it away in a comment for non-admins. There’s no need to ruin the design of your site for normal visitors.

It also tells you at what time the speed check rang. This means that if you’re running page caching, you can see when the cache last ran.