wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_media_library_mode › WordPress Function

Since7.2.0
Deprecatedn/a
› wp_get_media_library_mode ( No parameters )
Returns:
  • (string) The Media Library mode, 'grid' when none is saved.
Defined at:
Codex:

Returns the current Media Library mode (grid or list).

Replicates the mode resolution in wp-admin/upload.php, which runs after the load-upload.php hook, without updating the saved user option. upload.php falls back to grid mode for any falsey saved value and renders grid mode only for the exact value 'grid'. A truthy saved value outside the two known modes - one a plugin stored, say - therefore renders list mode there and resolves to 'list' here, so callers do not isolate a page that the Media Library renders in list mode.


Source

function wp_get_media_library_mode(): string {
	$valid_modes = array( 'grid', 'list' );

	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $valid_modes, true ) ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return $_GET['mode'];
	}

	$mode = get_user_option( 'media_library_mode', get_current_user_id() );

	if ( in_array( $mode, $valid_modes, true ) ) {
		return $mode;
	}

	// As in upload.php: nothing saved renders the grid, any other value the list.
	return $mode ? 'list' : 'grid';
}