To be honest I don’t fully understand why you need this custom code to upload images, it should be possible with the default settings.
To answer your question:
If you dig a bit through the code, you’ll see that
header_image()
callsget_header_image()
and returns thatget_header_image()
callsget_theme_mod('header_image', ...)
and will return that (as long as its notremove-header
oris_random_header_image()
returnstrue
).get_theme_mod()
now finally introduces a filter:theme_mod_{$name}
So, the following filter should be able to change the header image to whatever you want:
add_filter('theme_mod_header_image', function ($originalValue) {
// return URL to your desired image
});