Function defined in included file breaking wordpress - php

Here is my code:
single-location.php
<?php
// include start_dates.php
include('start_dates.php'); // contians the start dates and a helper function contains()
$crazy = crazyness_rad(); // breaks the page for some reason
start_dates.php
<?php
// simple test function that returns a string...
function crazyness_rad() {
return 'WLHLHDFLDHFLDHF KJDHF KJDHF KLJDHF K';
}
And when I call this function from single-location.php it breaks the page...this works outside of wordpress just fine.
Why if I call a function defined in an include does it break? Thanks for the help with my noob question.
The error I get is:
Fatal error: Call to undefined function crazyness_rad() in C:\inetpub\larockwww\wp-content\themes\larock-academy\single-location.php on line 51
I just moved the function from the start_dates.php into the single-location.php and it works fine:
single-location.php
<?php
// include start_dates.php
include('start_dates.php'); // contains the start dates and a
// Moved into this file
function crazyness_rad() {
return 'WLHLHDFLDHFLDHF KJDHF KJDHF KLJDHF K';
}
$crazy = crazyness_rad(); // works fine IT seems the include isn't working..

seems that file start_dates.php is not in the correct path. Please include with the absolute path of the file . You can also try get_template_part function to include the file.
https://developer.wordpress.org/reference/functions/get_template_part/

Related

PHP Fatal error: Call to undefined function?

So i have a problem with my website when im hosting it on my webhost.
i get this error:
PHP Fatal error: Call to undefined function getSkillIcons()
The weird thing is that locally(Xampp) it works just fine.
This is how i included the function(in index.php):
<?php include 'http://sub.website.com/incl/Settings.php'; ?>
this is where i call it (index.php):
<div class="panel-body" style="text-align:center">
<?php getSkillIcons(explode(',', skills)); ?>
</div>
This how i defined skills (settings.php)
define("skills", "Test1,Test2,Test3,Test4");
this is the function itself: (settings.php)
function getSkillIcons($skills) {
echo '<img src="http://sub.website.com/incl/img/skill_icons/Overall-icon.png" class="skill-icon">';
for ($i = 0; $i < count($skills); $i++) {
$tooltip = 'title="'.$skills[$i].'" data-toggle="tooltip" data-placement="top"';
echo '';
}
Call to undefined function error clearly shows that its not getting your function where you have defined. Reason is you are attaching full path of settings.php file with http.
You need to include settings.php file without http path at the top of 'index.php' and make sure settings.php file is located in your project only. If it is located at the same folder with index.php, then simply include like below.
<?php include __DIR__.'/settings.php'; ?>
If your settings.php file is located in some other folder then you can use $_SERVER['DOCUMENT_ROOT'] to include that file like below:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/settings.php";
include_once($path);
Also just an FYI for anyone trying to call a function within the same class in PHP, refer to the function like below:
return $this->doSomething($str);

how to access a php variable inside a function from an included file

I have a file called test1.php with lots of variable and some function definitions. I am trying to include this file to one another file called test2.php and use the variables and the functions.
test1.php
$i = "a";
$ii = "b";
$iii = "c";
function test1a(){ return "lol"; }
test2.php
function test2a(){ include 'test1.php'; return $i; }
function test2b() { include 'test1.php'; return test2c(); }
function test2c(){ include 'test1.php'; return $iii; }
function test2d() { include 'test1.php'; return test1a(); }
index.php
include 'test2.php'
echo test2a();
echo test2b();
echo test2c();
echo test2d();
Reason:
I have the same code base in two different servers. Only the test2.php file will be different.Each server will have different values inside the test2.php but with same variable name. test2.php will act somewhat like a localization file.
My problem is some of the variables or not showing up. Is there a better way to do this. I don't want to include the file in every function.
Thanks.
Just do it the other way round:
put all the different variables into one file in to an array, without any functions:
//config.php
$config['setting1'] = "val1";
$config['setting2'] = "val2";
$config['setting3'] = 42;
...
and further:
//index.php
include_once("config.php");
echo $config['setting1'];
....
now you may have different configs on different servers w/o need to change any functions.
You are trying to include file with one function several times. Functions are always global. So second include gives you an fatal error Fatal error: Cannot redeclare test1a() (previously declared in [..]).
You should put this function to separate file and use include_once.

PHP : Can not call object method from a page which included by another method of my class

I have just created a class to control my php application, and I have one big problem ( I use 2 days for thinking and searching about it but can't find any solutions). My class contains a method named register(), which load scripts into pages. My class is:
class Apps
{
protected $_remember; // remember something
public function register($appName)
{
include "$appName.php"; //include this php script into other pages
}
public function set($value)
{
$this->_remember = $value; // try to save something
}
public function watch()
{
return $this->_remember; // return what I saved
}
}
And in time.php file
$time = 'haha';
$apps->set($time);
As the title of my question , when I purely include time.php into main.php, I can use $apps->set($time) ($apps has been defined in main.php). Like this main.php:
$apps = new Apps();// create Apps object
include "time.php";
echo $apps->watch(); // **this successfully outputs 'haha'**
But when I call method register() from Apps class to include time.php , I got errors undefined variable $apps and call set method from none object for time.php (sounds like it doesn't accept $apps inside time.php to me) . My main.php is:
$apps = new Apps();// create Apps object
$apps->register('time'); // this simply include time.php into page and it has
//included but time.php doesn't accept $apps from main.php
echo $apps->watch(); // **this outputs errors as I said**
By the way , I'm not good at writing . So if you don't understand anything just ask me. I appreciate any replies. :D
If you want your second code snippet to work, replace the content of time.php with:
$time = 'haha';
$this->set($time); // instead of $apps->set($time);
since this code is included by an instance method of the Apps class, it will have access to the instance itself, $this.

'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:
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!
#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();
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'.

Undefined variable error on a variable inside an included file

I'm including this file to collect one of its variables. File path is correct and i don't get file include errors. But when i try to print a variable inside that file it gives undefined variable error. Following is the code.
include_once($path . "folder/file.php");
echo($code);
There is a php class. inside the class there is a login function . Within that function I'm including another file assume it's funtions.php.
functions.php has above code in it.
Inside functions.php file i include this file which contains the variable i'm looking for (assume it's test.php).
test.php looks like this (inside php tags)
$code="something";
$another_variable="something else";
so now like i said before when i include this inside functions.php and print $code it why does it gives an undefined error?
Full code
function log($user, $pass) {
global $config;
$this->get_available_accounts();
if (isset($this->Users_obj[$user])) {
if ($this->Users_obj[$user]->userName == $user && $this->Users_obj[$user]->passWord == $pass) {
delete_cache_full();
$_SESSION['username'] = $user;
$_SESSION['log'] = true;
$_SESSION['usergroup']=$this->Users_obj[$user]->level;
$this->set_permission_session($_SESSION['usergroup']);
include_once $config->path . 'config.php';
$newUpdate2=showUpdates2();
if(!empty($newUpdate2)){
$_SESSION['updateremindlater']=$newUpdate2['date'];
}
//file
include_once $config->path . 'functions.php';
$func = new UserFuncs();
$func->validate();
include_once $config->path . 'view/ViewDashboard.php';
return true;
}
}
thats the function im including this file into. include_once $config->$path . 'functions.php'; includes functions.php file
functions.php looks like this
include_once($path. "folder/config.php");
echo($code);
and config.php looks like
$code = "ERGERW2342HV3453WERWEER";
$host_name = "SERV345WERFDGDDFGDGF";
$return_code = "DFGDFSDGFS";
$vurl = "YUIYUJHGJ";
$next_val ="AWERFDFVDV";
$val_exp = "NMGHJGJGH";
Help much appreciated!
Just guessing that you've included config.php somewhere else before, and that it's not being included again due to include_once. Therefore the variables are not created in the scope you're including it in the second time.
This could be a problem with variable scope, where you are trying t access the variable from somewhere (e.g. a function) where it is not available to that particular object (you did mention that you included the file within a class).
I agree with deceze and hakre, it's very hard to answer this question without seeing your code.

Categories