Site logo

[Resolved] functions.php error

Home Forums Support [Resolved] functions.php error

Home Forums Support functions.php error

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1703279
    Den

    Hi team,

    Thanks for your 24×7 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

    #1703284
    Leo
    Staff
    Customer Support

    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/

    #1703287
    Den

    Hi leo,

    No, I am editing the Child Theme’s function.php

    (not the parent theme) 🙂

    #1703288
    Leo
    Staff
    Customer Support

    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.

    #1703293
    Den

    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.

    #1703297
    Leo
    Staff
    Customer Support

    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?

    #1703299
    Den

    Alright, not an issue

    Thanks 🙂

    #1703302
    Leo
    Staff
    Customer Support

    No problem 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.