Using WordPress ‘get_stylesheet()’ PHP function

The get_stylesheet() WordPress PHP function retrieves the name of the current stylesheet.

Usage

$current_stylesheet = get_stylesheet();
echo 'The current stylesheet is: ' . $current_stylesheet;

Parameters

  • None

More information

See WordPress Developer Resources: get_stylesheet

Examples

Display current stylesheet name

This example displays the name of the current stylesheet on the website.

$current_stylesheet = get_stylesheet();
echo 'The current stylesheet is: ' . $current_stylesheet;

Check if a specific stylesheet is active

This example checks if a specific stylesheet named ‘my-theme’ is currently active.

if (get_stylesheet() == 'my-theme') {
    echo 'My theme is active!';
} else {
    echo 'My theme is not active.';
}

Change body class based on the active stylesheet

This example adds a different body class based on the active stylesheet.

$body_class = 'default-body';
if (get_stylesheet() == 'my-theme') {
    $body_class = 'my-theme-body';
}
echo '<body class="' . $body_class . '">';

Load a specific stylesheet file based on the active theme

This example loads a custom CSS file only if the active stylesheet is ‘my-theme’.

if (get_stylesheet() == 'my-theme') {
    wp_enqueue_style('my-theme-custom', get_template_directory_uri() . '/custom.css');
}

Display a message in the admin area if a specific theme is not active

This example displays a warning message in the WordPress admin area if the active stylesheet is not ‘my-theme’.

function check_active_theme() {
    if (get_stylesheet() != 'my-theme') {
        echo '<div class="notice notice-warning is-dismissible">';
        echo '<p>Please activate the My Theme stylesheet for full functionality.</p>';
        echo '</div>';
    }
}
add_action('admin_notices', 'check_active_theme');

Tagged in

Leave a Comment

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