‘Call to undefined function’ – Trying to call function from an included file

So I have two files, ‘header.php’ and ‘pluginfile.php’

The function that I want to call resides in ‘pluginfile.php’ and is:

Read More
public function getNonSubscriptionAmount() {
    $total = 0;
    foreach($this->_items as $item) {
      if(!$item->isSubscription()) {
        $total += $item->getProductPrice() * $item->getQuantity();
      }
      else {
        // item is subscription
        $basePrice = $item->getBaseProductPrice();
        Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Item is a subscription with base price $basePrice");
        $total += $basePrice;
      }
    }
    return $total;
  }

So in ‘header.php’ I have:

<?php
include_once($_SERVER['DOCUMENT_ROOT']."/wp-content/plugins/plugin-name/folder/PluginFile.php");
print getNonSubscriptionAmount();
?>

This gives the following error when any page is loaded:

Fatal error: Call to undefined function getnonsubscriptionamount() in
/home/username/domain.com/wp-content/themes/theme/header.php on
line 72

I’ve spent a couple of hours now trying to figure this out alone and am getting nowhere! Any help much appreciated!

Related posts

Leave a Reply

2 comments

  1. @Wiseguy looks like he had the right idea put in the comments.

    You are declaring a method and not a function. Is the function the entirety of your plugin.php file or is there more? If it is everything, remove the public modifier and just declare

    function getNonSubscriptionAmount() {
      // code here
    }
    

    But from the looks of the code it is part of a larger class. If thats the case then @Wiseguy comment is right on, you need to instantiate a new object of the class in plugin.php and then the desired method.

    $obj = new PluginClass();
    $obj->getNonSubscriptionAmount();
    
  2. You said:

    The function that I want to call resides in ‘plugin.php’ and is:

    And in your file you are including:

    So in ‘header.php’ I have:
    include_once($_SERVER[‘DOCUMENT_ROOT’].”/wp-content/plugins/plugin-name/folder/PluginFile.php”);
    print getNonSubscriptionAmount();

    You are not including ‘plugin.php’ which is were the function lives.

    Header.php should include ‘plugin.php’, not ‘PluginFile.php’.