Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active April 17, 2016 12:49
Show Gist options
  • Save devinsays/7af792846055f8130f3b to your computer and use it in GitHub Desktop.
Save devinsays/7af792846055f8130f3b to your computer and use it in GitHub Desktop.
Code snippet for update script to use new theme logo
<?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' );
@ericnicolaas
Copy link

@devinsays This would work fine provided the user has upgraded WordPress before upgrading their theme. If they upgrade their theme before upgrading to WP 3.5 and then upgrade to 3.5, the logo would not be migrated across.

@ericnicolaas
Copy link

Here is a variation that avoids the use of a theme version check, instead just checking for the existence of the custom_logo mod: https://gist.github.com/ericnicolaas/fa64a99ef02f9efbcdf89345c74d6aff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment