Soft Reject, Enqueue the Google fonts the right way

wordpress

#1

We have received this issue as soft reject and we tried to adjust that and search from different resources on google and we have come up with this function:

//Google Font Enqueue

function rebuild_google_fonts() {
    
    $rebuild_data =  jx_rebuild_globals();
    
    $fonts_url = '';
     
    /* Translators: If there are characters in your language that are not
    * supported by Lora, translate this to 'off'. Do not translate
    * into your own language.
    */


    if($rebuild_data['google_body_font'] && $rebuild_data['google_body_font'] != 'Select Font'):
        $font_families[]= esc_attr($rebuild_data['google_body_font']). ":400,400italic,700,700italic:latin,greek-ext,cyrillic,latin-ext,greek,cyrillic-ext,vietnamese";
    endif;
    
    if($rebuild_data['google_menu_font'] && $rebuild_data['google_menu_font'] != 'Select Font' && $rebuild_data['google_menu_font'] != $rebuild_data['google_body_font']):
    $font_families[]= esc_attr($rebuild_data['google_menu_font']). ':400,400italic,700,700italic:latin,greek-ext,cyrillic,latin-ext,greek,cyrillic-ext,vietnamese';
    endif;
    
    if($rebuild_data['google_headings_font'] && $rebuild_data['google_headings_font'] != 'Select Font' && $rebuild_data['google_headings_font'] != $rebuild_data['google_body_font'] && $rebuild_data['google_headings_font'] != $rebuild_data['google_menu_font']):
    $font_families[]= esc_attr($rebuild_data['google_headings_font']). ':400,400italic,700,700italic:latin,greek-ext,cyrillic,latin-ext,greek,cyrillic-ext,vietnamese';
    endif;
    
     if($rebuild_data['google_footer_headings_font'] && $rebuild_data['google_footer_headings_font'] != 'Select Font' && $rebuild_data['google_footer_headings_font'] != $rebuild_data['google_body_font'] && $rebuild_data['google_footer_headings_font'] != $rebuild_data['google_menu_font'] && $rebuild_data['google_footer_headings_font'] != $rebuild_data['google_headings_font']):
    $font_families[]= esc_attr($rebuild_data['google_body_font']). ':400,400italic,700,700italic:latin,greek-ext,cyrillic,latin-ext,greek,cyrillic-ext,vietnamese';
    endif;
    
    if($rebuild_data['google_font_manual_load'] && $rebuild_data['google_font_manual_load'] != 'Select Font'):
    $font_families[]= esc_attr($rebuild_data['google_font_manual_load']). ':400,400italic,700,700italic:latin,greek-ext,cyrillic,latin-ext,greek,cyrillic-ext,vietnamese'; 
    endif;
        
        
         
    $query_args = array(
    'family' => urlencode( implode( '|', $font_families ) ),
    'subset' => urlencode( 'latin,latin-ext' ),
    );
         
    $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
    
     
    return esc_url_raw( $fonts_url );
}

//EOF

The fonts are loaded and we are using enquue like this

wp_enqueue_style( ‘google-fonts’, rebuild_google_fonts(), array(), null );
wp_enqueue_script(‘google-fonts’);

i Dont know if that is correct or not

But that seems incorrect even if its loading the google fonts, the issue here we have modified the function so we can load dynamic google fonts based on user selection from theme option.

So any help will be appreciated in this issue???


#2

what is that abomination :frowning:

why don’t you do this:

function google_fonts() {
	$query_args = array(
		'family' => 'Open+Sans:400,700|Oswald:700'
		'subset' => 'latin,latin-ext',
	);
	wp_register_style( 'google_fonts', add_query_arg( $query_args, "//fonts.googleapis.com/css" ), array(), null );
            }
            
add_action('wp_enqueue_scripts', 'google_fonts');