Skip to content

Font Library: "Fetch error: The response is not a valid JSON response" when filtering font_dir. #58696

@jazzsequence

Description

@jazzsequence

Description

When testing a filter to change the directory of downloaded fonts, the fonts are not installed and return an error instead. Using the example in #57697 as a base.

Example code:

/**
 * Define a custom font directory for the WP Font Library.
 */
function alter_wp_fonts_dir( $defaults ) {
	$wp_upload_dir = wp_get_upload_dir();
	$uploads_basedir = $wp_upload_dir['basedir'];
	$uploads_baseurl = $wp_upload_dir['baseurl'];

	$fonts_dir = $uploads_basedir . '/fonts';
	// Generate the URL for the fonts directory from the font dir.
	$fonts_url = str_replace( $uploads_basedir, $uploads_baseurl, $fonts_dir );

	$defaults['path'] = $fonts_dir;
	$defaults['url']  = $fonts_url;

	return $defaults;
}
add_filter( 'font_dir', 'alter_wp_fonts_dir' );

Step-by-step reproduction instructions

  1. Hook the code above to a function bootstrapped on init.
  2. Navigate to the Font Library
  3. Navigate to Install Fonts
  4. Pick a font to install
  5. Click the Install button

Screenshots, screen recording, code snippet

Loom recording

Console:
Screenshot 2024-02-05 at 3 08 10 PM

Environment info

  • WordPress 6.5-alpha-57531
  • PHP: tested on 7.4.33, 8.1.18, 8.2.5
  • Gutenberg: tested on 17.6.1, 17.6.2, 17.6.4
  • MacOS Sonoma 14.2.1
  • Google Chrome 121.0.6167.85

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Labels

[Feature] Font Library[Feature] TypographyFont and typography-related issues and PRs[Priority] HighUsed to indicate top priority items that need quick attention[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions