- This topic is empty.
-
AuthorPosts
-
December 26, 2015 at 4:12 am #236155AyanizeParticipant
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 thisif (get_template() != 'Theme-One'){
But not sure how to achieve this with two themes.
Thanks
Kalyan
December 26, 2015 at 5:32 am #236156AlenParticipantI 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.
AlenDecember 26, 2015 at 5:54 am #236157AyanizeParticipantHello 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
December 27, 2015 at 6:52 am #236193danallenParticipantIs 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.
December 27, 2015 at 6:57 am #236195AlenParticipantI 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; }
December 28, 2015 at 7:39 am #236215AyanizeParticipantThank 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.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.