1

new here and to wordpress plugins development, go easy on me :D

Anyway, I am trying to create a new plugin and I am getting a 500 error. I changed WP_DEBUG in config.php to true in order to see what is causing the 500 error and got this message:

Fatal error: Uncaught Error: Call to undefined function is_woocommerce() in...

This is my code currently:

<?php
/**
 * Plugin Name: 
 * Plugin URI: 
 * Description: 
 * Author: 
 * Author URI: 
 * Version: 1.0
 * Text Domain: 
 *
 * Copyright: (c) 2018
 *
 * License: 
 * License URI: 
 *
 * @author    
 * @copyright Copyright (c) 2018
 * @license   
 *
 */
//
defined( 'ABSPATH' ) or exit;
if (function_exists(is_woocommerce())) {
    echo "test: ".is_woocommerce();
} else {
    echo "test: Function does not exists!";
}

If you need any more information, tell me and I'll edit the question. Help will be appreciated, thanks!

  • you don't use function_exists correctly secure.php.net/function_exists – mmm Jul 22 '18 at 12:26
  • Fixed the use of function_exists but I am getting false on if (function_exists('is_woocommerce')) – Omer Jul 22 '18 at 13:44
  • WooCommerce won’t be loaded when you use the function naked like that. Make sure you’re only using that function in a template or a hook that occurs after all plugins are loaded. – Jacob Peattie Jul 22 '18 at 14:27
  • So if I want to create a plugin which relies on WooCommerce, how can I achieve it? for example if I want the plugin to do something only on product-category page? – Omer Jul 22 '18 at 16:50
  • 1
    if you want to be sure that woocommerce is active you can use if class_exists('woocommerce'){ // do something } OR is_multisite() ? $filter=get_blog_option(get_current_blog_id(), 'active_plugins' ) : $filter=get_option('active_plugins' ); if ( in_array( 'woocommerce', apply_filters( 'active_plugins', $filter) ) ) { //do something if woocommerce is active } if you want to do something in a template it's safe to overwrite the templates: docs.woocommerce.com/document/template-structure – Andrea Somovigo Jul 22 '18 at 20:09
2

If you want to check one Plugin's Function / Class etc. from another Plugin, then it's best to use a hook like plugins_loaded.

Based on this, your Plugin CODE will look like:

<?php
/*
Plugin Name: YOUR PLUGIN NAME
*/

defined( 'ABSPATH' ) or exit;

add_action( 'plugins_loaded', 'plugin_prefix_woocommerce_check' );
    function plugin_prefix_woocommerce_check() {
    if( function_exists( 'is_woocommerce' ) ) { 
        add_action( "wp_footer", "wpse_woocommerce_exists" );
    }   
    else {
        add_action( "wp_footer", "wpse_woocommerce_doesnt_exist" );
    }   
}   

function wpse_woocommerce_exists() {
    echo "<h1>WooCommerce Exists!</h1>";
}   

function wpse_woocommerce_doesnt_exist() {
    echo "<h1>WooCommerce Doesn't Exists!</h1>";
}

Directly checking other plugin functions will often lead to error, since WordPress may not be done loading other plugins by the time your CODE executes. However, when WordPress is done, it'll fire the plugins_loaded hook.

Check Plugin Development Guide for more information on how to develop a WordPress Plugin.

  • I am using the code you've provided but nothing happens, I tried looking for the word "test" in the page source but there is none. – Omer Jul 23 '18 at 19:02
  • Did you activate the plugin from WordPress admin panel? – Nazaria Jul 28 '18 at 1:04
  • Updated the CODE so that the test TEXT appears in the footer. Doing echo on plugins_loaded hook will print the text CODE before HTML starts generating. So that's not a good idea. The original CODE was posted just as a concept of using plugins_loaded hook. There are better ways to debug WordPress. Obviously echoing text is not a good debugging method. – Nazaria Jul 28 '18 at 1:08

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.