1

So I have two files, 'header.php' and 'pluginfile.php'

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

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!

9
  • 1
    You might try changing your include_once to a require_once which can make sure the file is being properly included in case there is something going on with the pathing. Commented Aug 17, 2012 at 17:55
  • 4
    public function ... This tells me it's defined as a class method, but you're calling it as though it's a standalone function. Commented Aug 17, 2012 at 17:55
  • good catch @Wiseguy, that would definitely cause problems. Commented Aug 17, 2012 at 17:56
  • 1
    You need to instantiate an object of that class and call it from the object. For example, $obj = new SomeClass; print $obj->getNonSubscriptionAmount(); More info about class methods in the docs here. Commented Aug 17, 2012 at 18:05
  • 1
    Glad you got it to work, but like I said, maybe the correct data won't be populated yet so you won't get useful values. :-/ I'd have to actually submit an answer for you to accept it. @SidewaysGravity said the same thing, so I'd suggest selecting his. I just added a bit of clarification. Commented Aug 17, 2012 at 20:15

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I'm still unsure of where I put this in my code, I just get errors!
0

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'.

2 Comments

apologies that was my mistake in writing the question!
Word, in that case my answer is of no help, hah.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.