Edit: google-fonts.php
<?php /** * Create a Google Fonts dropdown select element * * @param string $selected_font Optional. The currently selected font. * @param string $select_name Optional. The name attribute for the select element. * @param string $select_id Optional. The id attribute for the select element. * @param array $select_classes Optional. Array of classes to add to the select element. * @return string HTML for the font dropdown */ function dnext_button_fonts_dropdown( $selected_font = '', $select_name = 'button_font', $select_id = 'button_font', $select_classes = array( 'regular-text' ) ) { // Get site locale $site_domain = get_locale(); // Get Google fonts $google_fonts = et_builder_get_fonts( array( 'prepend_standard_fonts' => false, ) ); // Get custom fonts $user_fonts = et_builder_get_custom_fonts(); // Combine Google fonts with custom user fonts $google_fonts = array_merge( $user_fonts, $google_fonts ); // Define domain-specific character sets $et_domain_fonts = array( 'ru_RU' => 'cyrillic', 'uk' => 'cyrillic', 'bg_BG' => 'cyrillic', 'vi' => 'vietnamese', 'el' => 'greek', 'ar' => 'arabic', 'he_IL' => 'hebrew', 'th' => 'thai', 'si_lk' => 'sinhala', 'bn_bd' => 'bengali', 'ta_lk' => 'tamil', 'te' => 'telegu', 'km' => 'khmer', 'kn' => 'kannada', 'ml_in' => 'malayalam', ); // Initialize font choices array $font_choices = array(); $font_choices['none'] = array( 'label' => 'Default Theme Font', ); // Get font mapping for removed fonts $removed_fonts_mapping = function_exists( 'et_builder_old_fonts_mapping' ) ? et_builder_old_fonts_mapping() : array(); // Process each Google font foreach ( $google_fonts as $google_font_name => $google_font_properties ) { $use_parent_font = false; if ( isset( $removed_fonts_mapping[ $google_font_name ] ) ) { $parent_font = $removed_fonts_mapping[ $google_font_name ]['parent_font']; $google_font_properties['character_set'] = $google_fonts[ $parent_font ]['character_set']; $use_parent_font = true; } // Skip fonts that don't support the current locale's character set if ( '' !== $site_domain && isset( $et_domain_fonts[ $site_domain ] ) && isset( $google_font_properties['character_set'] ) && false === strpos( $google_font_properties['character_set'], $et_domain_fonts[ $site_domain ] ) ) { continue; } $font_choices[ $google_font_name ] = array( 'label' => $google_font_name, 'data' => array( 'parent_font' => $use_parent_font ? $google_font_properties['parent_font'] : '', 'parent_styles' => $use_parent_font ? $google_fonts[ $parent_font ]['styles'] : $google_font_properties['styles'], 'current_styles' => $use_parent_font && isset( $google_fonts[ $parent_font ]['styles'] ) && isset( $google_font_properties['styles'] ) ? $google_font_properties['styles'] : '', 'parent_subset' => $use_parent_font && isset( $google_fonts[ $parent_font ]['character_set'] ) ? $google_fonts[ $parent_font ]['character_set'] : '', 'standard' => isset( $google_font_properties['standard'] ) && $google_font_properties['standard'] ? 'on' : 'off', ), ); } // Build the select element $class_attr = ! empty( $select_classes ) ? ' class="' . esc_attr( implode( ' ', $select_classes ) ) . '"' : ''; $output = '<select name="' . esc_attr( $select_name ) . '" id="' . esc_attr( $select_id ) . '"' . $class_attr . '>'; // Add font options foreach ( $font_choices as $font_key => $font_data ) { $selected = selected( $selected_font, $font_key, false ); $data_attrs = ''; // Add data attributes if they exist if ( isset( $font_data['data'] ) && is_array( $font_data['data'] ) ) { foreach ( $font_data['data'] as $data_key => $data_value ) { $data_attrs .= ' data-' . esc_attr( $data_key ) . '="' . esc_attr( $data_value ) . '"'; } } $output .= '<option value="' . esc_attr( $font_key ) . '"' . $selected . $data_attrs . '>' . esc_html( $font_data['label'] ) . '</option>'; } $output .= '</select>'; return $output; } /** * Display the Google Fonts dropdown * * @param string $selected_font Optional. The currently selected font. * @param string $select_name Optional. The name attribute for the select element. * @param string $select_id Optional. The id attribute for the select element. * @param array $select_classes Optional. Array of classes to add to the select element. */ function dnext_display_google_fonts_dropdown( $selected_font = '', $select_name = 'button_font', $select_id = 'button_font', $select_classes = array( 'regular-text' ) ) { $output = dnext_button_fonts_dropdown( $selected_font, $select_name, $select_id, $select_classes ); // Add Select2 for enhanced dropdown with search wp_enqueue_style( 'select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css' ); wp_enqueue_script( 'select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', array( 'jquery' ), null, true ); // Add the JavaScript to initialize Select2 and load Google Fonts for preview $output .= ' <script type="text/javascript"> jQuery(document).ready(function($) { $("#' . esc_js( $select_id ) . '").select2({ templateResult: formatFontOption, }); // Load Google Fonts for the dropdown var fontsToLoad = []; $("#' . esc_js( $select_id ) . ' option").each(function() { var fontName = $(this).val(); if (fontName !== "none" && fontName !== "") { fontsToLoad.push(fontName); } }); function formatFontOption(font) { if (!font.id || font.id === "none") { return font.text; } return $("<span>").text(font.text).css("font-family", font.id); } }); </script>'; return $output; }
Save
Back