Archived topic
functions.php error
7 replies · Started by Den on March 20, 2021
Hi team,
Thanks for your 24x7 support.
The thing is, I'm getting an error while saving a code on my functions.php
THE CODE
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
THE ERROR THAT I AM GETTING
Your PHP code changes were rolled back due to an error on line 84 of file
wp-content/themes/generatepress/functions.php. Please fix and try saving again.
syntax error, unexpected 'var' (T_VAR)
What's the issue? I didn't quite get that. Can you please figure out how do I make it worked?
BTW, this is what i wanna achieve - https://www.w3schools.com/howto/howto_js_toggle_dark_mode.asp
Hi there,
Are you editing the theme's function.php?
It's never a good idea to do this as your code will be erased during updates.
Can you revert the file to the original state first?
https://github.com/tomusborne/generatepress/blob/master/functions.php
This is something that should be done in the function.php file of a child theme:
https://docs.generatepress.com/article/using-child-theme/
Hi leo,
No, I am editing the Child Theme's function.php
(not the parent theme) :)
Hmm the error you are showing me above is indicating that the error is in the parent theme's function.php:
wp-content/themes/generatepress/functions.php
And there is definitely no error in line 84:
https://github.com/tomusborne/generatepress/blob/master/functions.php#L84
Can you make sure the parent theme's function.php matches the file I linked?
Or you can reinstalling the theme.
Nope, nothing's wrong with my parent theme's function.php (I've just checked it)
It's just when I try to add this code...
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
- on my website, it just won't let me save that changes because there's some error in var element = document.body; that I'm getting.
See here - https://ibb.co/dbFkCpm
(now it's on the line 49)
------------------------------------------------------
I just want to enable "Toggle Dark Mode" on my website.
and to do this, as per w3schools.com,
I have to add a CSS
body {
padding: 25px;
background-color: white;
color: black;
font-size: 25px;
}
.dark-mode {
background-color: black;
color: white;
}
and a javascript
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
to enable this function.
We can't help with custom functions here as it's not part of the theme:
https://generatepress.com/what-support-includes/
But looks like you might have extra endif; at line 53?
Alright, not an issue
Thanks :)
No problem :)