Skip to content

Webfonts API: Return font family slug when registering a webfont#40120

Merged
zaguiini merged 2 commits intotrunkfrom
update/return-font-family-slug-when-registering-webfont
Apr 7, 2022
Merged

Webfonts API: Return font family slug when registering a webfont#40120
zaguiini merged 2 commits intotrunkfrom
update/return-font-family-slug-when-registering-webfont

Conversation

@zaguiini
Copy link
Copy Markdown
Member

@zaguiini zaguiini commented Apr 6, 2022

What?

Return the font family slug when registering a webfont.

Why?

There are cases where you want to be aware of the font family slug. One example is https://github.com/Automattic/jetpack/pull/23810/files#diff-67965f8a19767c6775dd0d0949ed5830c22bda270ec3c1835f801649e98596c2R70.

Returning the slug removes the need to call WP_Webfonts::get_font_slug after registering a webfont.

How?

Simply returning the $slug variable from the register_webfont method is enough.

Testing Instructions

You should check that the font family slug is returned after calling wp_register_webfont and wp_register_webfonts.

@zaguiini zaguiini force-pushed the update/return-font-family-slug-when-registering-webfont branch from 608efc3 to 4d84d18 Compare April 7, 2022 00:01
@jeyip
Copy link
Copy Markdown
Contributor

jeyip commented Apr 7, 2022

Testing

Requirements

  • wp_register_webfont returns the registered slug string
  • wp_register_webfonts returns the array of registered slug strings
// I tested this PR by error logging the output of wp_register_webfont and 
// wp_register_webfonts in the functions.php file of the currently active theme
$slug = wp_register_webfont(
		array(
			'font-family'  => 'Roboto',
			'font-style'   => 'normal',
			'font-stretch' => 'normal',
			"font-weight" => "900",
			'src'          => array( 'file:./assets/fonts/Roboto-Regular.ttf' ),
		)
	);

// Outputs 'roboto'
error_log( $slug );
$slugs = wp_register_webfonts(array(
		array(
			'font-family'  => 'Roboto',
			'font-style'   => 'normal',
			'font-stretch' => 'normal',
			"font-weight" => "900",
			'src'          => array( 'file:./assets/fonts/Roboto-Regular.ttf' ),
		)
	));

// Outputs [ 'roboto' ]
error_log( $slugs );

Copy link
Copy Markdown
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@zaguiini zaguiini merged commit f26587a into trunk Apr 7, 2022
@zaguiini zaguiini deleted the update/return-font-family-slug-when-registering-webfont branch April 7, 2022 14:22
@github-actions github-actions bot added this to the Gutenberg 13.0 milestone Apr 7, 2022
@bph bph added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Dev Note Requires a developer note for a major WordPress release cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants