Skip to content

Instantly share code, notes, and snippets.

@hasanm95
Created January 2, 2018 15:13
Show Gist options
  • Save hasanm95/5a7c9c8ac567343f8f3a08a61b8fa54c to your computer and use it in GitHub Desktop.
Save hasanm95/5a7c9c8ac567343f8f3a08a61b8fa54c to your computer and use it in GitHub Desktop.
Include google font with codestarframework
// theme options
// set options for google fonts
function industry_rrfonline_theme_options( $options ) {
$options = array(); // remove old options
$options[] = array(
'name' => 'Typography',
'title' => 'Typography',
'icon' => 'fa fa-heart',
'fields' => array(
array(
'id' => 'body_font',
'type' => 'typography',
'title' => 'Body Font',
'default' => array(
'family' => 'Montserrat',
'variant' => '300',
),
),
array(
'id' => 'body_font_variant',
'type' => 'checkbox',
'title' => 'Body Font Variants',
'default' => array('300', '300i', 'regular', 'italic', '500', '500i', '600', '600i', '700', '700i'),
'options' => array(
'300' => '300',
'300i' => '300 Italic',
'regular' => '400',
'italic' => '400 Italic',
'500' => '500',
'500i' => '500 Italic',
'600' => '600',
'600i' => '600 Italic',
'700' => '700',
'700i' => '700 Italic',
'800' => '800',
'800i' => '800 Italic',
'900' => '900',
'900i' => '900 Italic',
)
),
array(
'id' => 'body_font_size',
'type' => 'text',
'title' => 'Body Font Size',
'default' => '14px'
),
array(
'id' => 'different_heading_font',
'type' => 'switcher',
'title' => 'Different Heading Font?',
'default' => false,
),
array(
'id' => 'heading_font',
'type' => 'typography',
'title' => 'Heading Font',
'default' => array(
'family' => 'Montserrat',
'variant' => '300',
),
'dependency' => array('different_heading_font', '==', 'true'),
),
array(
'id' => 'heading_font_variant',
'type' => 'checkbox',
'title' => 'Heading Font Variants',
'default' => array('300', '300i', 'regular', 'italic', '500', '500i', '600', '600i', '700', '700i'),
'options' => array(
'300' => '300',
'300i' => '300 Italic',
'regular' => '400',
'italic' => '400 Italic',
'500' => '500',
'500i' => '500 Italic',
'600' => '600',
'600i' => '600 Italic',
'700' => '700',
'700i' => '700 Italic',
'800' => '800',
'800i' => '800 Italic',
'900' => '900',
'900i' => '900 Italic',
),
'dependency' => array('different_heading_font', '==', 'true'),
),
)
);
return $options;
}
add_filter( 'cs_framework_options', 'industry_rrfonline_theme_options' );
//fuctions.php
//Google fonts function
function industry_rrf_online_fonts_url() {
$fonts_url = '';
$fonts = array();
$subsets = 'latin,latin-ext';
$body_font = cs_get_option('body_font');
$body_font_variants_array = cs_get_option('body_font_variant');
$body_font_variants = implode(',', $body_font_variants_array);
$body_font_family = $body_font['family'];
$heading_font = cs_get_option('heading_font');
$heading_font_family = $heading_font['family'];
$different_heading_font = cs_get_option('different_heading_font');
$heading_font_variants_array = cs_get_option('heading_font_variant');
$heading_font_variants = implode(',', $heading_font_variants_array);
if(!empty($body_font)){
$body_font_family = $body_font['family'];
}else{
$body_font_family = 'Monterrat';
}
if(!empty($heading_font)){
$heading_font_family = $heading_font['family'];
}else{
$heading_font_family = 'Monterrat';
}
$fonts[] = ''.esc_attr($body_font_family).':'.esc_attr($body_font_variants).'';
if($different_heading_font == true){
$fonts[] = ''.esc_attr($body_font_family).':'.esc_attr($heading_font_variants).'';
}
if ( $fonts ) {
$fonts_url = add_query_arg( array(
'family' => urlencode( implode( '|', $fonts ) ),
'subset' => urlencode( $subsets ),
), 'https://fonts.googleapis.com/css' );
}
return $fonts_url;
}
//Enqueue Google fonts
function rrf_industry_scripts() {
wp_enqueue_style( 'industry-rrf-online-fonts', industry_rrf_online_fonts_url(), array(), null );
}
add_action( 'wp_enqueue_scripts', 'rrf_industry_scripts' );
//theme-style.php
//inline css
function industry_rrfonline_custom_css(){
$body_font = cs_get_option('body_font');
$body_font_size = cs_get_option('body_font_size');
$heading_font = cs_get_option('heading_font');
$heading_font_family = $heading_font['family'];
$different_heading_font = cs_get_option('different_heading_font');
if(!empty($body_font)){
$body_font_family = $body_font['family'];
}else{
$body_font_family = 'Monterrat';
}
wp_enqueue_style('industry-rrfonline-custom', get_template_directory_uri(). '/assets/css/custom.css');
$custom_css = '';
$custom_css .= '
body{
font-family: '.esc_html($body_font_family).';
font-size: '.esc_attr($body_font_size).';
font-weight: '.esc_attr($body_font['variant']).';
}
';
if($different_heading_font == true){
$custom_css .= '
h1, h2, h3, h4, h5, h6{
font-family: '.esc_html($heading_font_family).';
font-weight: '.esc_attr($heading_font['variant']).';
}
';
}
wp_add_inline_style('industry-rrfonline-custom', $custom_css);
}
add_action('wp_enqueue_scripts', 'industry_rrfonline_custom_css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment