Created
June 28, 2022 07:30
-
-
Save ipokkel/ba32c0142d3a8344ebff8b11714dfd64 to your computer and use it in GitHub Desktop.
Load Memberlite fonts from Bunny.net.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Load Memberlite fonts from Bunny.net instead of Google Fonts. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
/** | |
* Replace Google fonts with Bunny.net fonts. | |
*/ | |
function my_memberlite_replace_google_with_bunny_fonts() { | |
// Check if Memberlite is active. | |
if ( ! defined( 'MEMBERLITE_VERSION' ) ) { | |
return; | |
} | |
remove_action( 'wp_enqueue_scripts', 'memberlite_load_google_fonts' ); | |
add_action( 'wp_enqueue_scripts', 'memberlite_load_bunny_fonts' ); | |
} | |
add_action( 'init', 'my_memberlite_replace_google_with_bunny_fonts' ); | |
/** | |
* Enqueue fonts via Bunny.net API | |
*/ | |
function memberlite_load_bunny_fonts() { | |
// Check if Memberlite is active. | |
if ( ! defined( 'MEMBERLITE_VERSION' ) ) { | |
return; | |
} | |
// Get the encoded URL of the Google Fonts (if set). | |
$fonts_url = memberlite_bunny_fonts_url(); | |
if ( ! empty( $fonts_url ) ) { | |
wp_enqueue_style( 'memberlite-bunny-fonts', $fonts_url, array(), MEMBERLITE_VERSION ); | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'memberlite_load_bunny_fonts' ); | |
function memberlite_bunny_fonts_url() { | |
global $memberlite_defaults; | |
// Get the selected fonts from theme options. | |
$fonts_string = get_theme_mod( 'memberlite_webfonts', $memberlite_defaults['memberlite_webfonts'] ); | |
// Check if custom font is a Google Font. | |
if ( ! in_array( $fonts_string, array_keys( Memberlite_Customize::get_google_fonts() ) ) ) { | |
return null; | |
} else { | |
// Build the encoded Google fonts URL. | |
$fonts_url = ''; | |
// Filter to modify which font weights are enqueued. | |
$font_weights = apply_filters( 'memberlite_google_fonts_weights', '400,700' ); | |
$font_weights = apply_filters( 'memberlite_bunny_fonts_weights', $font_weights ); | |
// Build the array of font families to return. | |
$font_families = array(); | |
$font_families[] = memberlite_get_font( 'header_font', true ) . ':' . $font_weights; | |
$font_families[] = memberlite_get_font( 'body_font', true ) . ':' . $font_weights; | |
$query_args = array( | |
'family' => urlencode( implode( '|', $font_families ) ), | |
'subset' => urlencode( 'latin,latin-ext' ), | |
); | |
$fonts_url = add_query_arg( $query_args, '//fonts.bunny.net/css' ); | |
return esc_url_raw( $fonts_url ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment