WP_Theme::get_allowed_on_site( int $blog_id = null ): string[]

In this article

Returns array of stylesheet names of themes allowed on the site.

Parameters

$blog_idintoptional
ID of the site. Defaults to the current site.

Default:null

Return

string[] Array of stylesheet names.

Source

public static function get_allowed_on_site( $blog_id = null ) {
	static $allowed_themes = array();

	if ( ! $blog_id || ! is_multisite() ) {
		$blog_id = get_current_blog_id();
	}

	if ( isset( $allowed_themes[ $blog_id ] ) ) {
		/**
		 * Filters the array of themes allowed on the site.
		 *
		 * @since 4.5.0
		 *
		 * @param string[] $allowed_themes An array of theme stylesheet names.
		 * @param int      $blog_id        ID of the site. Defaults to current site.
		 */
		return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
	}

	$current = get_current_blog_id() == $blog_id;

	if ( $current ) {
		$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
	} else {
		switch_to_blog( $blog_id );
		$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
		restore_current_blog();
	}

	/*
	 * This is all super old MU back compat joy.
	 * 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
	 */
	if ( false === $allowed_themes[ $blog_id ] ) {
		if ( $current ) {
			$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
		} else {
			switch_to_blog( $blog_id );
			$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
			restore_current_blog();
		}

		if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
			$allowed_themes[ $blog_id ] = array();
		} else {
			$converted = array();
			$themes    = wp_get_themes();
			foreach ( $themes as $stylesheet => $theme_data ) {
				if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get( 'Name' ) ] ) ) {
					$converted[ $stylesheet ] = true;
				}
			}
			$allowed_themes[ $blog_id ] = $converted;
		}
		// Set the option so we never have to go through this pain again.
		if ( is_admin() && $allowed_themes[ $blog_id ] ) {
			if ( $current ) {
				update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
				delete_option( 'allowed_themes' );
			} else {
				switch_to_blog( $blog_id );
				update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
				delete_option( 'allowed_themes' );
				restore_current_blog();
			}
		}
	}

	/** This filter is documented in wp-includes/class-wp-theme.php */
	return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
}

Hooks

apply_filters( ‘site_allowed_themes’, string[] $allowed_themes, int $blog_id )

Filters the array of themes allowed on the site.

Changelog

VersionDescription
3.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.