Function and usage of get_site_allowed_themes() in wordpress

Question

Answers ( 1 )

    0
    2024-01-07T20:35:09+00:00

    The get_site_allowed_themes() function in WordPress is a deprecated function that was previously used to retrieve the list of themes that were network-enabled on a WordPress multisite installation. In a multisite setup, network administrators have the ability to enable or disable themes for use across the network. This function provided a way to fetch the themes that were allowed on the entire network.

    However, as it is now deprecated, it's recommended not to use get_site_allowed_themes() in new developments. Instead, the recommended approach is to use WP_Theme::get_allowed_on_network(). This is a method of the WP_Theme class, which is a more modern and updated way to achieve the same functionality.

    Here's a basic usage example of how you might have previously used get_site_allowed_themes() and how you should now use WP_Theme::get_allowed_on_network():

    Deprecated Usage of get_site_allowed_themes()

    // This is a deprecated way of getting network-enabled themes
    $allowed_themes = get_site_allowed_themes();
    foreach ($allowed_themes as $theme_slug => $theme_name) {
        echo 'Theme: ' . $theme_name . ' (Slug: ' . $theme_slug . ')';
    }
    

    Recommended Usage of WP_Theme::get_allowed_on_network()

    // This is the recommended way to get network-enabled themes
    $allowed_themes = WP_Theme::get_allowed_on_network();
    foreach ($allowed_themes as $theme_slug => $theme_name) {
        echo 'Theme: ' . $theme_name . ' (Slug: ' . $theme_slug . ')';
    }
    

    In the recommended approach, WP_Theme::get_allowed_on_network() is used to get an array of the allowed themes. The structure of the code is similar, but it utilizes the WP_Theme class, which is more consistent with the current WordPress coding standards and practices. This helps ensure better compatibility with future WordPress updates and features.

Leave an answer