Support » Plugins » Hacks » Detect If Theme Is Enabled From A Plugin

  • Resolved spartanv7

    (@praeix)


    Hello all.

    I’m trying to see if there’s a way to detect if a particular theme is installed and enabled from a plugin. I know that the opposite can be done (detect a plugin from a theme), but is this possible?

    I’ve already attempted to define a constant in the theme that I could check if it was defined from the plugin. However, that doesn’t work and I’m pretty sure it’s because the order of how WordPress loads (plugins before themes). So the questions I have are:

    1. Can the theme load a constant earlier in the queue?
    2. Can the plugin somehow interact with the theme beforehand to see if it’s enabled? (What could be done to see if the theme at least exists on the file system could be to see if the directory exists, but that won’t tell whether the theme is actually the current active theme…right?)
    3. Should I use the settings API and store the constant in there upon theme activation so that the plugin could read from the database?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Thread Starter spartanv7

    (@praeix)

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

    Thread Starter spartanv7

    (@praeix)

    resolved

    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.

    Thread Starter spartanv7

    (@praeix)

    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 🙂

    Thread Starter spartanv7

    (@praeix)

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

    Thread Starter spartanv7

    (@praeix)

    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.

    Thread Starter spartanv7

    (@praeix)

    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.

    Thread Starter spartanv7

    (@praeix)

    Here’s my logic on it:

    1. User enables your theme
    1. Create option for theme
    2. Set option to ‘enabled’/’true’/whatever
    • User downloads your plugin and activates
    • Plugin checks if theme is active
    1. If ‘yes’ – Do code for theme enabled
    2. If ‘no’ – Do nothing
    • User switches themes to ‘TwentyEleven’
    1. Before the switch, your theme switches option to ‘disabled’ or even better, deletes the option altogether
    • User switches back to your theme – start over at step 1

    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.

    Thread Starter spartanv7

    (@praeix)

    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?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Detect If Theme Is Enabled From A Plugin’ is closed to new replies.