Tuesday 11 February 2014

Condition for plugin active - is_plugin_active

condition check for plugin if is active plugin wordpress
Condition check for plugin - is_plugin_active

Recently When I was creating a theme for wordpress using Bootstrap 3. I decided to add bootstrap CPT Carousel plugin.

When I added the shortcode in the homepage it worked properly but when plugin is not active it prints the short code [image-carousel"] on the homepage.

The solution I found was checking if the plugin is active or not.



It is a simple but very interesting check for the plugin to find if the plugin you are looking to work in your theme is active or not. use the following code for condition check for plugin active.



<?php
  // Include path to the Plugin directory.
  // This snippet only works for front end.
  include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

  // check for plugin using plugin name
  if ( is_plugin_active( 'cpt-bootstrap-carousel/cpt-bootstrap-carousel.php' ) ) {
    echo do_shortcode('[image-carousel"]');
  }
?>

inside the is_plugin_active there is one path first is the folder name of the plugin second is the file name. some plugins has same folder name but has index.php inside the folder. other plugins are without folder like hello.php

Codex Function reference for is_plugin_active

No comments :

Post a Comment