0

I'm currently building a framework that enhances a few things such as:

  1. Custom Customizer Controls
  2. Custom Single Post Layouts
  3. etc.

For example, this means that on my single.php I call framework\GetCustomPostLayout(), the problem with that is my framework is a plugin which needs to be installed, so errors will show if it's not enabled.

I'm looking for a way to do

if( function_exists( 'Framework' ) ) { 
    get_framework_behavior
} else {
    get_nonframework_behavior_default 
}

Unfortunately, even this is transformed into a wrapper such as:

getCustomPostLayout that checks within itself that the plugin is activated, I'd have to write my default behavior inside of it, making me put logic that's supposed to be in single.php in another file.

How could I best approach this?

0

How about using the WordPress is_plugin_active() function? - https://codex.wordpress.org/Function_Reference/is_plugin_active

This is an admin only function though, so to use it in the frontend or within a template file you would need to manually require plugin.php like so:

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
is_plugin_active($plugin);

Where $plugin is the name of the plugin sub directory/file eg:

is_plugin_active( 'plugin-directory/plugin-file.php' );

| improve this answer | |
  • That did come up, yes, but that was the issue - only back-end. I'll wait for another answer, I'm not too sure function_exists is as clean and perhaps there's a better way. – Jonathan C Mar 29 '18 at 14:37
  • The above function can be used in the frontend by requiring the plugin.php file first, as per my example. I've also seen class_exists used to check if WooCommerce is active/installed before... If function_exists works for you then I think this would be fine and cleaner than using the is_plugin_active function... Don't think you're going to find any 'cleaner' way of checking... – Mat Mar 29 '18 at 14:51
  • I'd also be inclined to question, that if the functionality is specific to your theme, then why are you adding the functionality with a plugin instead of coding the functionality directly in to your themes code? Or is your plugin intended to be used on multiple sites and then site owners/theme authors would need to add conditional code to their template files to utilise the functionality of your plugin? – Mat Mar 29 '18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.