Using WordPress ‘icon_dir’ PHP filter

The icon_dir WordPress PHP Filter allows you to modify the icon directory path.

Usage

add_filter('icon_dir', 'your_custom_function_name');
function your_custom_function_name($path) {
    // your custom code here
    return $path;
}

Parameters

  • $path (string) – The icon directory’s absolute path.

More information

See WordPress Developer Resources: icon_dir

Examples

Change the Icon Directory Path

Change the default icon directory path to a custom directory within your theme.

add_filter('icon_dir', 'change_icon_directory');
function change_icon_directory($path) {
    $path = get_stylesheet_directory() . '/custom-icons';
    return $path;
}

Append a Subdirectory to the Icon Directory

Add a subdirectory to the default icon directory path.

add_filter('icon_dir', 'append_icon_subdirectory');
function append_icon_subdirectory($path) {
    $path .= '/custom';
    return $path;
}

Use a Custom Directory for a Specific Theme

Use a custom icon directory only for a specific theme.

add_filter('icon_dir', 'theme_specific_icon_directory');
function theme_specific_icon_directory($path) {
    if (get_template() == 'my-theme') {
        $path = get_template_directory() . '/my-icons';
    }
    return $path;
}

Modify the Icon Directory Based on User Role

Change the icon directory path based on the current user’s role.

add_filter('icon_dir', 'user_role_based_icon_directory');
function user_role_based_icon_directory($path) {
    if (current_user_can('administrator')) {
        $path = get_stylesheet_directory() . '/admin-icons';
    }
    return $path;
}

Apply the Change Only on Specific Pages

Change the icon directory path only on specific pages, such as the home page.

add_filter('icon_dir', 'specific_page_icon_directory');
function specific_page_icon_directory($path) {
    if (is_front_page()) {
        $path = get_stylesheet_directory() . '/front-page-icons';
    }
    return $path;
}