How to Add Dark Mode to WordPress Website

How to Add Dark Mode to Your WordPress Website

Dark mode has become popular across websites and applications due to its benefits in reducing eye strain, improving battery life, and providing a sleek, modern look. If you run a WordPress website, enabling dark mode can enhance the user experience and keep visitors engaged longer.

This guide explores why you should add dark mode to your WordPress website and the different methods to implement it, including using plugins and custom coding.

Why Add Dark Mode to Your WordPress Website?

dark mode sample
  • Enhanced User Experience: Many users prefer dark mode, which reduces eye strain, especially in low-light environments. Websites offering light and dark modes allow visitors to choose the most comfortable option.
  • Battery Efficiency: Dark mode on OLED and AMOLED screens can significantly reduce power consumption. This is particularly important for mobile users who browse websites for extended periods.
  • Aesthetic Appeal: Dark mode gives websites a modern and stylish look. It can complement various design elements and create a unique experience for users.
  • Accessibility and Inclusivity: Some users are sensitive to bright screens or have medical conditions that require dark mode. Providing this option makes your website more inclusive.
  • Improved Engagement: Users who find a website visually comfortable are likelier to stay longer, explore content, and engage with your website.

Methods to Add Dark Mode in WordPress

There are two primary ways to implement dark mode in WordPress:

  1. Using a plugin (recommended for beginners)
  2. Manually adding dark mode with CSS and JavaScript (for advanced users)

Method 1: Adding Dark Mode with a Plugin (Beginner-Friendly)

Plugins make it easy to add dark mode without writing code. Below are some of the best plugins to enable dark mode on your WordPress website.

Step 1: Choose a Dark Mode Plugin

Some popular plugins include:

wp dark mode wordpress plugin
  • WP Dark Mode: A feature-rich plugin with automatic dark mode, customizable switch buttons and  more.
  • Darklup Lite: Provides easy toggle buttons and deep customization.

Step 2: Install and Activate the Plugin

wp dark mode plugin
  1. Log into your WordPress dashboard.
  2. Navigate to Plugins > Add New.
  3. Search for your preferred dark mode plugin.
  4. Click Install Now, then Activate.

Step 3: Configure Plugin Settings

  1. Go to WP Dark Mode > Settings (or the plugin’s dedicated settings page).
wp dark mode dashboard
  1. Enable dark mode for your website.
  2. Customize the appearance, toggle button, and color settings, and press Save Changes.
dark mode preview

Most plugins allow users to switch between dark and light modes with a simple toggle button, making it convenient for visitors.

Method 2: Manually Adding Dark Mode (For Advanced Users)

You can achieve this using CSS and JavaScript if you prefer a custom dark mode solution. This method provides greater flexibility and avoids relying on third-party plugins.

Step 1: Add a Dark Mode Toggle Button

You need a toggle button to allow users to switch between light and dark modes. Add the following HTML inside your theme’s header (header.php) or sidebar (sidebar.php).

<button id="dark-mode-toggle">Dark Mode</button>
button html code

This button will trigger dark mode when clicked.

Step 2: Add Dark Mode Styles with CSS

Now, create a CSS class for dark mode. Add the following to your theme’s style.css file.

body.dark-mode {

    background-color: #121212;

    color: #ffffff;

}

.dark-mode a {

    color: #1e90ff;

}

.dark-mode header, 

.dark-mode footer {

    background-color: #1a1a1a;

}

.dark-mode button {

    background-color: #333;

    color: #fff;

    border: 1px solid #555;

}
additional css code

When dark mode is enabled, these styles adjust the background, text, buttons, and links. 

Step 3: Use JavaScript to Enable Dark Mode

Add the following JavaScript code in your footer.php file or a custom JavaScript file to allow users to switch between light and dark modes.

document.addEventListener("DOMContentLoaded", function() {

    const darkModeToggle = document.getElementById("dark-mode-toggle");

    const body = document.body;

    // Check if the dark mode was previously enabled

    if (localStorage.getItem("darkMode") === "enabled") {

        body.classList.add("dark-mode");

    }

    darkModeToggle.addEventListener("click", function() {

        body.classList.toggle("dark-mode");

        // Save user preference

        if (body.classList.contains("dark-mode")) {

            localStorage.setItem("darkMode", "enabled");

        } else {

            localStorage.removeItem("darkMode");

        }

    });

});
footer php code

This script does the following:

  • Toggles dark mode when the button is clicked.
  • Saves the user’s preference in local storage.
  • Restores dark mode if the user had previously enabled it.

Step 4: Add Custom Styling for the Toggle Button

For better appearance, add the following CSS to your style.css file:

#dark-mode-toggle {

    position: fixed;

    bottom: 20px;

    right: 20px;

    padding: 10px 20px;

    background-color: #0073aa;

    color: white;

    border: none;

    cursor: pointer;

    border-radius: 5px;

}

#dark-mode-toggle:hover {

    background-color: #005a87;

}
dark mode toggle button

Testing and Troubleshooting

After implementing dark mode, test it thoroughly to ensure smooth functionality.

Testing Steps

  • Open your website and try the toggle button.
  • Refresh the page to confirm if the dark mode setting persists.
  • Check different pages to ensure styles apply consistently.
  • Test on multiple devices and browsers.

Troubleshooting Tips

  • If the dark mode does not persist after the refresh, ensure localStorage is enabled in the browser.
  • If certain elements remain bright, inspect them using the browser’s developer tools (F12 or Ctrl + Shift + I).
  • Check for compatibility issues with your theme or other plugins if using a plugin.

Enhancing Dark Mode with Advanced Features

If you want to take dark mode further, consider these enhancements:

1. Auto Dark Mode Based on System Preference

Modern browsers support a prefers-color-scheme media query that detects the user’s system preference.

Add this CSS to apply dark mode for users who have it enabled automatically:

@media (prefers-color-scheme: dark) {

    body {

        background-color: #121212;

        color: #ffffff;

    }

}

2. Scheduled Dark Mode

If you want your website to switch to dark mode at night automatically, use JavaScript to check the time.

const currentHour = new Date().getHours();

if (currentHour >= 18 || currentHour < 6) {

    document.body.classList.add("dark-mode");

}

This enables dark mode from 6 PM to 6 AM.

3. Dark Mode for Logged-in Users Only

If you run a membership site or blog with registered users, you may want to enable dark mode only for logged-in users. You can do this by modifying your theme’s functions.php file.

if (is_user_logged_in()) {

    echo '<script>document.body.classList.add("dark-mode");</script>';

}

Conclusion

Adding dark mode to your WordPress website dramatically improves user experience, accessibility, and aesthetics. Whether you choose a plugin for ease of use or prefer a custom-coded solution for more control, implementing dark mode can enhance your site’s appeal and usability.

Plugins like WP Dark Mode provide most users with a quick and efficient solution. However, if you want more flexibility, the manual CSS and JavaScript method gives you complete control over the design and functionality.

Following this guide, you can successfully integrate dark mode into your WordPress site and offer visitors a more comfortable browsing experience.

Related Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.