View Categories

WP Dark Mode: PHP Hooks

This guide lists all available Action and Filter Hooks in the WP Dark Mode plugin, with examples and best practices for extending or customizing its functionality.


πŸ”§ Action Hooks #

1. Core Plugin Actions #

/**

 * Fires when the dark mode plugin is fully loaded

 */

do_action( 'wp_dark_mode_loaded' );

// Example:

add_action( 'wp_dark_mode_loaded', 'my_custom_dark_mode_init' );

function my_custom_dark_mode_init() {

    add_custom_dark_mode_styles();

    register_custom_dark_mode_settings();

}

2. Social Share Actions #

/** Fires before the social share buttons are rendered */

do_action( 'before_wpdm_social_share' );

/** Fires after the social share buttons are rendered */

do_action( 'after_wpdm_social_share' );

// Example:

add_action( 'before_wpdm_social_share', 'add_custom_social_share_header' );

function add_custom_social_share_header() {

    echo '<div class="custom-share-header">Share this page:</div>';

}

3. Theme Support Actions #

/**

 * Fires when a theme supports dark mode

 * @param string $theme_slug The current theme's slug

 */

do_action( 'wp_dark_mode_theme_supports', $theme_slug );

// Example:

add_action( 'wp_dark_mode_theme_supports', 'custom_theme_dark_mode_setup' );

function custom_theme_dark_mode_setup( $theme_slug ) {

    if ( 'my-custom-theme' === $theme_slug ) {

        add_theme_specific_dark_mode_styles();

    }

}

πŸŽ›οΈ Filter Hooks #

1. Core Configuration Filters #

/** Filter the default dark mode options */

apply_filters( 'wp_dark_mode_default_options', $options );

// Example:

add_filter( 'wp_dark_mode_default_options', 'custom_dark_mode_options' );

function custom_dark_mode_options( $options ) {

    $options['custom_setting'] = 'value';

    return $options;

}

2. Style and Script Filters #

/** Filter the dark mode inline styles */

apply_filters( 'wp_dark_mode_inline_styles', $css );

/** Filter the dark mode inline scripts */

apply_filters( 'wp_dark_mode_inline_scripts', $js );

// Example:

add_filter( 'wp_dark_mode_inline_styles', 'add_custom_dark_mode_styles' );

function add_custom_dark_mode_styles( $css ) {

    $css .= '.custom-element { background-color: #1a1a1a; color: #ffffff; }';

    return $css;

}

3. Element Exclusion Filters #

/** Filter whether an element should be excluded from dark mode */

apply_filters( 'wp_dark_mode_is_excluded', false );

/** Filter the list of excluded elements */

apply_filters( 'wp_dark_mode_excluded_elements', $excluded );

// Example:

add_filter( 'wp_dark_mode_excluded_elements', 'custom_excluded_elements' );

function custom_excluded_elements( $excluded ) {

    $excluded[] = '.my-custom-element';

    $excluded[] = '#specific-id';

    return $excluded;

}

4. Theme Support Filters #

/** Filter the list of supported themes */

apply_filters( 'wp_dark_mode_supported_themes', $themes );

// Example:

add_filter( 'wp_dark_mode_supported_themes', 'add_custom_theme_support' );

function add_custom_theme_support( $themes ) {

    $themes[] = 'my-custom-theme';

    return $themes;

}

5. Social Share Filters #

/** Filter the social share count */

apply_filters( 'wpdm_social_share_count', $count );

/** Filter the social share channels */

apply_filters( 'wpdm_social_share_channels', $channels );

// Example:

add_filter( 'wpdm_social_share_channels', 'custom_social_channels' );

function custom_social_channels( $channels ) {

    $channels['custom_platform'] = [

        'name' => 'Custom Platform',

        'icon' => 'custom-icon',

        'url'  => 'https://custom-platform.com/share'

    ];

    return $channels;

}

6. Performance Filters #

/** Filter whether scripts should be loaded in footer */

apply_filters( 'wp_dark_mode_loads_scripts_in_footer', $load_in_footer );

// Example:

add_filter( 'wp_dark_mode_loads_scripts_in_footer', '__return_true' );

7. Preset Styles Filters #

/** Filter the dark mode preset styles */

apply_filters( 'wp_dark_mode_preset_styles', $styles );

// Example:

add_filter( 'wp_dark_mode_preset_styles', 'custom_preset_styles' );

function custom_preset_styles( $styles ) {

    $styles['custom_preset'] = [

        'name'   => 'Custom Preset',

        'colors' => [

            'bg'   => '#1a1a1a',

            'text' => '#ffffff'

        ]

    ];

    return $styles;

}

🧠 Best Practices #

βœ… Hook Priority #

Ensure your filter runs at the right moment:

add_filter( 'wp_dark_mode_excluded_elements', 'my_custom_elements', 20 );

πŸš€ Performance #

Cache output-heavy filters:

add_filter( 'wp_dark_mode_inline_styles', 'cached_custom_styles' );

function cached_custom_styles( $css ) {

    static $cached_css = null;

    if ( null === $cached_css ) {

        $cached_css = generate_custom_styles();

    }

    return $css . $cached_css;

}

πŸ”’ Security #

Escape all output:

add_filter( 'wp_dark_mode_inline_styles', 'secure_custom_styles' );

function secure_custom_styles( $css ) {

    $custom_color = esc_attr( get_theme_mod( 'custom_dark_color', '#000000' ) );

    return $css . ".custom-element { background-color: {$custom_color}; }";

}

🀝 Compatibility #

Check for function availability:

add_filter( 'wp_dark_mode_default_options', 'conditional_custom_options' );

function conditional_custom_options( $options ) {

    if ( function_exists( 'wp_dark_mode_is_ultimate' ) && wp_dark_mode_is_ultimate() ) {

        $options['premium_feature'] = 'value';

    }

    return $options;

}

πŸ’‘ Summary #

These hooks give you deep customization power over:

  • Dark mode appearance & behavior
  • Social share controls
  • Theme & plugin integrations
  • Custom CSS/JS enhancements
  • Performance optimization

Make sure to follow WordPress standards and test thoroughly when adding your customizations!

Add your first comment to this post