Using WordPress ‘get_screen_icon()’ PHP function

The get_screen_icon() WordPress PHP function retrieves the screen icon, which is no longer used in WordPress 3.8 and later versions.

Usage

get_screen_icon();

Parameters

  • None

More information

See WordPress Developer Resources: get_screen_icon()

This function is no longer used in WordPress 3.8 and later versions.

Examples

Displaying the screen icon (for older versions)

If you are using a version of WordPress prior to 3.8, you can use this function to display the screen icon.

// Display the screen icon
echo get_screen_icon();

Check WordPress version before using the function

To ensure backward compatibility, you can check the WordPress version before using the function.

// Check if the WordPress version is less than 3.8
if (version_compare(get_bloginfo('version'), '3.8', '<')) {
    // Display the screen icon
    echo get_screen_icon();
}

Custom function to display screen icon

Create a custom function to display the screen icon, which can be used throughout your theme or plugin.

function display_screen_icon() {
    // Check if the WordPress version is less than 3.8
    if (version_compare(get_bloginfo('version'), '3.8', '<')) {
        // Display the screen icon
        echo get_screen_icon();
    }
}

// Use the custom function to display the screen icon
display_screen_icon();

Custom screen icon using a filter

Use a filter to change the screen icon based on the current user’s role.

function custom_screen_icon($screen_icon) {
    // Get the current user
    $user = wp_get_current_user();

    // Check if the user has the 'administrator' role
    if (in_array('administrator', $user->roles)) {
        $screen_icon = 'custom-admin-icon.png';
    }

    return $screen_icon;
}
add_filter('get_screen_icon', 'custom_screen_icon');

// Display the custom screen icon
echo get_screen_icon();

Custom screen icon using a CSS class

Add a CSS class to the screen icon based on the current user’s role.

function custom_screen_icon_class($classes) {
    // Get the current user
    $user = wp_get_current_user();

    // Check if the user has the 'editor' role
    if (in_array('editor', $user->roles)) {
        $classes .= ' custom-editor-icon';
    }

    return $classes;
}
add_filter('screen_icon_classes', 'custom_screen_icon_class');

// Display the screen icon with the custom CSS class
echo get_screen_icon();