There are several ways to add custom JavaScript functions to your WordPress website:
- Using the Custom HTML widget:
- Go to Appearance > Widgets in the WordPress Admin panel.
- Add the Custom HTML widget to your sidebar or footer.
- Add your custom JavaScript code to the widget.
- Using a plugin:
- Search for a plugin called “Custom CSS and JS” or “Header and Footer Scripts” in the WordPress plugin repository.
- Install and activate the plugin.
- Go to the plugin’s settings page and add your custom JavaScript code to the header or footer section.
- Adding the JavaScript code directly to your theme’s header or footer:
- Go to Appearance > Editor in the WordPress Admin panel.
- Find the theme’s header or footer file (usually named header.php or footer.php).
- Add your custom JavaScript code to the appropriate section of the file.
- Save the changes.
Note: You should keep the theme’s header and footer the same because your changes will be lost when the theme is updated. Instead, you should use either the first or second method to ensure that your custom JavaScript remains even after updating the theme.