I’m trying to intercept the install themes routine in order to alert the user from the active theme. I’m cobbling together a crude javascript alert below. What are some other options?
if ( is_admin() && $pagenow == 'theme-install.php' && $_GET['tab']=="upload"){
?>
<script type="text/javascript">
alert('Message here');
</script>
<?php
}
The reason for this is to make sure that the user is not attempting to install my theme’s upgrader.zip upgrade file as a theme (which results in a broken theme).
I’m usually a fan of doing this kind of work on the server-side since it’s far too easy to circumvent JavaScript on the client-side with modern debugging tools, but this is a unique problem and this may fit your use case (depending on who your audience is).
Using pure JavaScript, you can do this:
Add the following code to it (and be sure to read the comments):
Add the following to your theme’s functions.php. This will load the script into your theme:
If you’re looking to go pure server-side, there’s no hook for managing the theme activation action; however, you may be able to take advantage of the
switch_theme
action. This is pure speculation but once the action fires you may be able to read some information from within the file then revert back to the previously used theme.Late answer
There’re two (nearly unknown) hooks for this in core:
Theme activation hook:
'after_switch_theme'
Theme de-activation hook:
'switch_theme'