Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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' );

@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.

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