Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Theme dependent plugin

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #236155
    Ayanize
    Participant

    Hi,

    I am creating a WP plugin which will require either of two themes so that it can work as it’s using the theme functions inherited from either of the plugins. I wrote this conditional check

    register_activation_hook(__FILE__, 'plugin_activation_check');
    
    
    function plugin_activation_check()
    {
     if (get_template() != 'Theme-One' || get_template() !='Theme-Two'){
            deactivate_plugins('theme-folder/my-plugin.php');
    
      /* message to user when they try to activate the plugin */
        wp_die('This plugin needs either Theme-One or Theme-two themes to be active.');
    
    }
    }
    
    

    I want that the plugin can only be activated if either of two recommended themes are active. Else, deactivate that plugin and return an error message.

    I can easily achieve this for one theme by writing the if condition like this

    if (get_template() != 'Theme-One'){

    But not sure how to achieve this with two themes.

    Thanks

    Kalyan

    #236156
    Alen
    Participant

    I am creating a WP plugin which will require either of two themes so that it can work

    Why? I would never use your plugin. Why do I have to use theme other than my own?

    as it’s using the theme functions inherited from either of the plugins

    Functions inherited from theme? or plugin? Why not extract the functions you need and decouple your code.


    $supported_themes = array('theme-1', 'theme-1'); if ( ! in_array( wp_get_theme(), $supported_themes ) ) { add_action( 'admin_notices', function() { echo '<div class="error"><p>Activate Supported Theme</p></div>'; } ); return; }

    Hope that helps.
    Alen

    #236157
    Ayanize
    Participant

    Hello Allen,

    Thanks for your reply. As I can see my code, I can find some typos and issues.

    This line should be deactivate_plugins('plugin-folder/my-plugin.php');

    And I forgot to add the filter which is

    add_action ('admin_init', 'plugin_activation_check');

    Now coming to your question, my plugin is using a page-builder which is available only in the theme (Elegant Themes Divi & Extra). Now I need to use the Page builder else this will not work. Now the problem is, not all themes will have that page-builder, of course. So, I just want to get the plugin deactivated if those themes (Divi or Extra) are not found since without that my plugin features will be useless and also, the site may crash as it’s calling a theme function.

    Anyway, I am just jumping to your solution and see if it helps. Give you an update once tried.

    Thanks a lot

    #236193
    danallen
    Participant

    Is it possible to extract the page-builder from Elegant Themes Divi & Extra to build it into your plugin? That sounds to me like it could be quite a challenge. I can understand your plan to restrict your plugin to those themes.

    What does the plugin do?

    As for testing for the presence of a theme, wp_get_theme() gives all the properties of the active theme, including its name. You just need to test whether the theme name of returned by wp_get_theme() is one of themes you are looking for.

    I would imagine your plugin needs to know if the active theme is one of the ones you need, not just that the theme is installed.

    Why were you puzzled by this requirement, it seems very straight forward and you seem to know what what you are doing. I feel like I must not be understanding the question.

    #236195
    Alen
    Participant

    I just want to get the plugin deactivated if those themes (Divi or Extra) are not found since without that my plugin features will be useless and also, the site may crash as it’s calling a theme function.

    I’m not familiar with that specific page builder. But I’m guessing it’s a PHP class. So instead of testing for the name of the theme, test if the class exists. So something like…

    if ( ! class_exists( 'PageBuilderClassHere' ) ) {
      echo 'You are not good enough for me!';
      return;
    }
    
    #236215
    Ayanize
    Participant

    Thank you both for your replies. I got it working. I was stupid in making the condition illogical. It’s simple I need to apply this

    `if ( get_template() != ‘Theme-One’ && get_template() !=’Theme-Two’ ) {

    `

    instead of

    if ( get_template() != 'Theme-One' || get_template() !='Theme-Two' ) {

    Now it triggers the deactivate_plugins filter when both themes are inactive.


    @danallen
    Elegant Themes has already released the Divi builder as a standalone plugin here it is http://www.elegantthemes.com/plugins/divi-builder/.

    My forthcoming plugin is simple. It will create a custom post type where I need to use the Divi Page Builder. Since not all themes won’t have that, I want my plugin to remain deactivated. There are others features however with this plugin which I am planning.

    Thanks for your time and support once again.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.