wppaste
WordPress

wp_generate_block_templates_export_file(): WP_Error|string

Since
5.9.0, 6.0.0
Source
wp-includes/block-template-utils.php:1480
Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.

Compatibility

WordPress
since 6.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

WP_Error|string
Path of the ZIP file or error on failure.

Performance profile

How much work a call to wp_generate_block_templates_export_file() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
13–111

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 178.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_generate_block_templates_export_file()
  • optionoption read or writeget_option()one call below wp_generate_block_templates_export_file()

Further down the call graph this can also reach transient, cache, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_generate_block_templates_export_file() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always13wp_get_wp_version(), __()
always42wp_get_wp_version(), wp_generate_password(), get_stylesheet(), basename(), get_temp_dir(), ->open(), __()
!$theme_json_raw97–100wp_get_wp_version(), wp_generate_password(), get_stylesheet(), basename(), get_temp_dir(), ->open(), ->addEmptyDir(), ->addEmptyDir(), get_stylesheet_directory(), wp_normalize_path(), get_block_templates(), get_block_templates(), ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->merge(), ->get_data(), wp_json_encode(), ->addFromString(), ->close()
$theme_json_raw108–111wp_get_wp_version(), wp_generate_password(), get_stylesheet(), basename(), get_temp_dir(), ->open(), ->addEmptyDir(), ->addEmptyDir(), get_stylesheet_directory(), wp_normalize_path(), get_block_templates(), get_block_templates(), ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->merge(), ->get_data(), array_merge(), wp_json_encode(), ->addFromString(), ->close()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17813–11111
8.517813–11111
8.417813–1111112 fewer instructions than PHP 8.3
8.319015–12011
8.219015–12011
8.119015–12011
7.419015–12011

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 18

Show all 18

Used by · 1

Source code

function wp_generate_block_templates_export_file() {	$wp_version = wp_get_wp_version(); 	if ( ! class_exists( 'ZipArchive' ) ) {		return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.' ) );	} 	$obscura    = wp_generate_password( 12, false, false );	$theme_name = basename( get_stylesheet() );	$filename   = get_temp_dir() . $theme_name . $obscura . '.zip'; 	$zip = new ZipArchive();	if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {		return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.' ) );	} 	$zip->addEmptyDir( 'templates' );	$zip->addEmptyDir( 'parts' ); 	// Get path of the theme.	$theme_path = wp_normalize_path( get_stylesheet_directory() ); 	// Create recursive directory iterator.	$theme_files = new RecursiveIteratorIterator(		new RecursiveDirectoryIterator( $theme_path ),		RecursiveIteratorIterator::LEAVES_ONLY	); 	// Make a copy of the current theme.	foreach ( $theme_files as $file ) {		// Skip directories as they are added automatically.		if ( ! $file->isDir() ) {			// Get real and relative path for current file.			$file_path     = wp_normalize_path( $file );			$relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); 			if ( ! wp_is_theme_directory_ignored( $relative_path ) ) {				$zip->addFile( $file_path, $relative_path );			}		}	} 	// Load templates into the zip file.	$templates = get_block_templates();	foreach ( $templates as $template ) {		$template->content = traverse_and_serialize_blocks(			parse_blocks( $template->content ),			'_remove_theme_attribute_from_template_part_block'		); 		$zip->addFromString(			'templates/' . $template->slug . '.html',			$template->content		);	} 	// Load template parts into the zip file.	$template_parts = get_block_templates( array(), 'wp_template_part' );	foreach ( $template_parts as $template_part ) {		$zip->addFromString(			'parts/' . $template_part->slug . '.html',			$template_part->content		);	} 	// Load theme.json into the zip file.	$tree = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) );	// Merge with user data.	$tree->merge( WP_Theme_JSON_Resolver::get_user_data() ); 	$theme_json_raw = $tree->get_data();	// If a version is defined, add a schema.	if ( $theme_json_raw['version'] ) {		$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 );		$schema             = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' );		$theme_json_raw     = array_merge( $schema, $theme_json_raw );	} 	// Convert to a string.	$theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.0.0
Adds the whole theme to the export archive.from the docblock
5.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/block-template-utils.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.