We have a setup where bloggers can and will have their theme changed. Is it possible to migrate the existing header image to the new theme ? I know how to override the header image with the add_filter, also know how to get the current header image. The issue here is to keep the header image when changing the theme. Is that possible ?


Here comes the plugins to rescue, move that code into a plugin and activate. That'll not change header image upon every theme change.

E.g -

Plugin Name: keep header always

// Your code to keep header image


save it as whatever-foo.php upload to /wp-content/plugins/ directory and activate it

| improve this answer | |
  • could you give me a short example on how that could be acomplished ? – Tonny Jørgensen Aug 9 '12 at 14:01
  • updated with example.. – amit Aug 9 '12 at 14:09
  • arh yes i know how to make a plugin, the code to NOT change the header image on theme change is what i need. – Tonny Jørgensen Aug 9 '12 at 14:32
  • the same one you used in functions.php move it to plugin. btw you said you know the code... – amit Aug 9 '12 at 14:35

The header image is set per …

set_theme_mod( 'header_image', $choice['url'] )

… a wrapper for …

update_option( 'theme_mods_' . get_option( 'stylesheet' ), $value );

So you can hook into …

'pre_update_option_theme_mods_' . get_option( 'stylesheet' )

… take the first parameter, which is an array of all theme mods, and save the value of the key 'header_image' in a plugin option. The key 'header_image_data' stores the meta data for that image.

Then filter 'theme_mod_header_image' to return your stored plugin option if there is no image set.

| improve this answer | |
  • So far i got this pastebin.com/E4GzH0X4 sorry had to use pastebin or else this comment was to long. The problem is this also hooks in when people are trying to change the header image, this is not what i want, only to save the header image on theme_switch, is there any other way to catch it BEFORE it changes ? – Tonny Jørgensen Aug 13 '12 at 12:15
  • The problem was solved with this code btw: – Tonny Jørgensen Jan 23 '13 at 13:21
  • pastebin.com/kjuWJBb0 – Tonny Jørgensen Jan 23 '13 at 13:22
  • The solution is in the pastebin url, but i will gladly post it here too. in short the solution is to put the header image reference in an option value and after the themeswitch then restore the value again. I cant post the source code here, as there is not enough charaters to post it :-) – Tonny Jørgensen Jan 24 '13 at 14:26

We did a small mu-plugin that solved our issue.

Here is the code

add_action('pre_update_option_stylesheet', 'phi_pre_update_option_stylesheet');
add_action('switch_theme', 'phi_switch_theme');

function phi_pre_update_option_stylesheet($stylesheet)
    update_option('previous_header_image_saved', get_theme_mod('header_image'));

    return $stylesheet;

function phi_switch_theme()
        if (get_option('previous_header_image_saved')) {
                set_theme_mod('header_image', get_option('previous_header_image_saved'));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.