@esmi – Thanks for the response. This is very close to what I needed, but ultimately I’m using child themes so the name could change to whatever the user wanted…
I instead decided to use add_option
and get_option
and it worked like a charm.
Again thanks for your response!
Care to share your code? Via http://wordpress.pastebin.com/ if it’s quite a big chunk? I miss get_theme() and I’ve been looking around for a way to replicate some of its functionality myself. Again, I want it to work with child themes.
Sure! It’s only a few lines, but basically this is what I do in my admin.php file that spools together the theme admin interface.
function fmg_constants() {
// Add the theme option for use in plugins
if ( !get_option( 'fmg_theme' ) ) {
// Set the option
if ( function_exists( 'add_option' ) ) add_option( 'fmg_theme', 'enabled' );
} else {
// Update the option
update_option( 'fmg_theme', 'enabled' );
}
}
Then in the plugin, I just check to see if it can pull in the option value.
$option = get_option( 'fmg_theme' );
if ( !empty( $option ) && $option == 'enabled' ) {
// Stuff to do if it's enabled
} else {
// Stuff to do if it doesn't exist or doesn't match 'enabled'
}
I think that might have saved me some time. Thanks 🙂
@esmi – Sorry there’s a flaw in the logic here. If you enable a theme like TwentyEleven, it still keeps the option value where it was set. I need to have a method for deleting the option on de-activation…
On deactivation of what? The plugin or the theme? That’s possible via the plugin but not a theme. There’s still no function similar to unregister_setting() for themes.
It would be the deactivation of the theme that should trigger the deletion of the option (so as not to build up crappy data, but to also serve as a reliable trigger to remove the option when your theme no longer exists), but as you stated, there is no deactivation hook or anything.
So I found this http://core.trac.wordpress.org/ticket/7795.
Krishna claims to have solved it, but I can’t seem to wrap my mind around the logic of the deactivation hook. Could be sleep-deprivation too LOL.
Um – that’s a 4 year old ticket. I think a fair amount will have changed since then. I know that the Theme API in general is starting to get some attention, so I hope that we will be seeing some improved functionality in future updates. Some decent theme deactivation hooks/functions have been at the top of the theme developers’ Wish List for a while.
Can you point me at Krishna’s post on this? I must have missed it.
Yeah I just happened to see that Krishna’s post is from 2011 though. Here’s the link.
Activation / Deactivation Hook For WordPress Theme
Not sure that I’d feel comfortable relying on an external script – especially one that has to be bundled on a per-theme basis.
Your logic is great but… there’s nothing to hook into for 4a. That’s what’s missing in the Theme API. Plugins can do this kind of thing using register_deactivation_hook. But there’s nothing similar for themes probably because you don’t deactivate a theme as such. You just change to another theme.
As it stands, the user would have to manually change the theme option to “disabled” before switching to Twenty Eleven. Something needs to be added to core so that as one theme is activated, core checks to see what the previous theme was and runs any deactivation routines.
Right, but what I think Krishna’s functions are supposed to be doing (have no idea if they do or not, some people in his comments claim it works, others don’t and I’m starting to test it right now) is adding the hook for you to use.
See the last line of his function wp_register_theme_deactivation_hook
? It says add_action( 'switch_theme', $fn );
Wouldn’t that suffice since it’s executing the user-defined function to delete the option on ‘switch_theme’? switch_theme only detects when the theme switches, however, if the logic is followed correctly, then when it switches to something like Twenty Eleven, then it doesn’t matter because the option gets deleted and the hooks aren’t in Twenty Eleven, so the plugin conditional would show ‘false’. If you switch to another of your own theme with the hooks registered, then it would just start the process all over again, setting the options correctly.
I’m just getting everything setup, so I’m going to test this out.
I agree that this needs to be addressed in the core because it seems like a pretty basic functionality. How does code for the core have to get approved?