MY SETUP (AND CODE)
- Wordpress Version 4.4.3 (Latest at time of writing)
- Installed on a local server using XAMPP
My homepage is separated into several sections, and the Theme Customizer has checkboxes to show and hide each section.
- When the section is to be hidden, I use jQuery's
.remove()to remove it.
- When the section is to be shown, I use the following code:
$('#section-cont-id').load(theme_folder + '/file.php')
theme_folder is the directory of the theme, and
file.php is the section I want to load.
By default, the section is shown (the setting is set to
true). Un-ticking the checkbox to hide the section works fine, it gets
.removeed. However, when I try and show the section by re-ticking the checkbox, it does not work as planned.
file.php has a
<style> at the start, with one little bit of PHP code. Then there are various other elements,
<h1>, <h3>, <p> etc. When the section is meant to get shown, I instead get an empty, white box. I used Inspect Element to view the code and found something interesting:
<style> tag gets loaded, but the rest of the elements do not, they do not even exist. There is only one line of PHP that in the
<style> tag, it gets a setting for the background of that section like so:
<?php echo get_theme_mod('setting'); ?>. When I look at the CSS for the section background image, it shows the following:
url('<br /> <b>Fatal error</b>: Call to undefined function get_theme_mod() in <b>C:XAMPPhtdocsWPwp-contentthemesmythemefile.php</b> on line <b>7</b><br />')
It seems the function
get_theme_mod() is not defined? Also I am not sure why the path was
echoed with no backslashes, but that may be Firefox Inspector Tools removing them. The correct path should be:
The elements after the
<style> are set out like follows:
<h1><?php echo get_theme_mod('setting1'); ?></h1> <h3><?php echo get_theme_mod('setting2'); ?></h3> <p><?php echo get_theme_mod('setting3'); ?></p>
However, they do not even appear as empty (no content, e.g.
<h1></h1>). They do not exist at all.
So my question is:
How can I remove the
Fatal error: Call to undefined function... and get the rest of
file.php to load correctly
What I think is happening:
First of all, I think the elements after the
<style> do not exist because, after the PHP code throws the error, the rest of the file is not loaded.
However, all the code in
file.php is inside a
<div>, and that div closing tag loads. So maybe it loads elements and closing tags using DOM, rather than line-by-line?
To deal with the error of
get_theme_mod() not being defined, here is my theory:
- (FACT) WordPress registers
get_theme_mod()in a file called
theme.php(as mentioned in the Codex, full path is
I think WordPress includes that file, then all the theme files, and after they have all been loaded,
theme.php is removed. (I don't exactly think PHP works like that, but I think that is what happening)
My evidence to back this up is:
Fatal Error: Call to undefined function.... If the function is not defined, the file must not be loaded
This is very different to the other questions on
Fatal error: Call to undefined function.... I am not getting the error when the page loads, whether the section is shown or not. I am only getting the error in the Theme Customizer when I try and show the section (from previously being hidden)