Using WordPress ‘get_site()’ PHP function

The get_site() WordPress PHP function retrieves site data given a site ID or site object.

Usage

get_site( $site );

Example:

Input: get_site( 2 );

Output: Returns the site data for the site with ID 2.

Parameters

  • $site (WP_Site|int|null) – Optional. Site to retrieve. Default is the current site. Default: null.

More information

See WordPress Developer Resources: get_site()

Examples

Retrieve the current site data

Retrieve the data for the current site.

$current_site = get_site();

Retrieve data for a specific site using its ID

Retrieve the data for the site with ID 3.

$site_data = get_site( 3 );

Retrieve the site URL

Get the site URL for the site with ID 4.

$site = get_site( 4 );
$site_url = $site->siteurl;

Check if a site exists by ID

Check if a site exists using its ID (e.g. ID 5).

$site_exists = ( get_site( 5 ) !== null );

Get the site name of the current site

Retrieve the site name for the current site.

$current_site = get_site();
$site_name = $current_site->blogname;