Theme Update Script for Custom Logo

I’ve been updating several of my themes to support the new custom logo feature that is being introduced in WordPress 4.5 (read about it here).

Most of my themes already had a logo option, so part of adding support for this new feature has been to build an update script to migrate the previously saved value (generally saved to the theme mod “logo”), to the new theme mod “custom_logo”.

I assume a lot of other theme authors will also be doing this, so I’m sharing my update code in case it can save someone a few minutes.

A few things to note:

  • You’ll likely need to tweak it to match your current theme_mod/setting for the logo.
  • I saved my logo value as a URL, so this script converts it to an attachment ID before saving it into the new theme_mod
<?php
/**
* Theme Update Script
*
* Runs if version number saved in theme_mod "version" doesn't match current theme version.
*/
function prefix_update_check() {
$ver = get_theme_mod( 'version', false );
// Return if update has already been run
if ( version_compare( $ver, '2.0.0' ) >= 0 ) {
return;
}
// If a logo has been set previously, update to use logo feature introduced in WordPress 4.5
if ( function_exists( 'the_custom_logo' ) && get_theme_mod( 'logo', false ) ) {
// Since previous logo was stored a URL, convert it to an attachment ID
$logo = attachment_url_to_postid( get_theme_mod( 'logo' ) );
if ( is_int( $logo ) ) {
set_theme_mod( 'custom_logo', attachment_url_to_postid( get_theme_mod( 'logo' ) ) );
}
remove_theme_mod( 'logo' );
}
// Update to match your current theme version
set_theme_mod( 'version', '2.0.0' );
}
add_action( 'after_setup_theme', 'prefix_update_check' );

If you see any improvements that can be made, feel free to edit the Gist!

About Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Nano. Find me on twitter @devinsays.

3 Responses

  1. This piece of code is OK for Custom Logo feature for the existing theme. But if someone is an using option panel like Redux, they can’t use this code so how they can manage it easily?

Leave a Reply