Auto Dark Mode in WordPress – Without Plugin


Implementing auto-dark mode in WordPress without a plugin involves custom CSS and possibly some PHP modifications. Here’s a step-by-step guide to help you achieve this:

1. Create a Custom CSS File

1. Access Your Theme’s Files:

  • Log in to your WordPress site via FTP or use the File Manager in your hosting control panel.
  • Navigate to wp-content/themes/your-theme-name.

2, Create a New CSS File:

  • Create a new file named custom.css (or any other name you prefer) in the theme directory.

3. Add Dark Mode Styles:

  • Open the custom.css file and add your dark mode styles. For example, you can use media queries to switch between light and dark modes based on user preference.
/* Custom CSS for Dark Mode */
@media (prefers-color-scheme: dark) {
    body {
        background-color: #121212;
        color: #ffffff;
    }
    a {
        color: #fff;
    }
    /* Or you can using invert for image or color */
    div {
        filter: invert(1);
    }
    /* Rest of your code */
}

You can use a filter:

hue-rotate(deg)Applies a hue rotation on the image. The value defines the number of degrees around the color circle the image samples will be adjusted. 0deg is the default and represents the original image.

Note: Maximum value is 360deg.
invert(%)Applies a hue rotation on the image. The value defines the number of degrees around the color circle the image samples will be adjusted. 0deg is the default and represents the original image.

Note: Maximum value is 360deg.
For filter: invert(%). the result will invert the image’s color, text, or background. 
If using filter: invert(1) hue-rotate(180), the result is the same color as the original but more black-darkest color.

2. Enqueue Custom CSS

Add Custom CSS and JS Files in Functions.php:

  • Open your theme’s functions.php file and add the following code to enqueue your custom CSS and JS files.
// Enqueue custom CSS
function enqueue_custom_styles() {
    wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/custom.css', array(), '1.0', 'all' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_styles' );

By following these steps, you can implement a basic auto-dark mode feature in your WordPress site without using any plugins. This approach allows you to customize the appearance of your site according to your needs and preferences.
Next Post Previous Post
No Comment
Add Comment
comment url