To add code to the <head>
section of your WordPress site via the functions.php
file, you can use the wp_head
action hook. Here’s a simple way to do this:
- Access your WordPress Dashboard: Log in to your WordPress admin panel.
- Navigate to Theme Editor: Go to Appearance > Theme Editor.
- Select the functions.php file: On the right sidebar, find and click on the
functions.php
file of your active theme. - Add your code: Insert the following code snippet at the end of the
functions.php
file:
function add_code_to_head() { ?> <!-- Your custom code goes here --> <script> // Example: Custom JavaScript code console.log('Hello, World!'); </script> <link rel="stylesheet" href="https://example.com/style.css"> <?php } add_action('wp_head', 'add_code_to_head');
Replace the comment and example code with your actual code, such as JavaScript, CSS links, or meta tags.
- Save Changes: After adding your code, click the “Update File” button to save your changes.
- Test Your Site: Visit your website to ensure that the code has been successfully added to the
<head>
section.
Always remember to back up your functions.php
file before making changes, as errors can break your site. If you’re unsure, consider using a child theme or a site-specific plugin for custom code.
Discover more from Be-smart
Subscribe to get the latest posts sent to your email.