Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

how to get google font link ?

Open xperter opened this issue 11 years ago • 6 comments

hello i have tried to use google font from theme options. i have added typography and can printed value in frontend. but how i get google font link in header ?? in your manual only available option type. but i cant catch google font link from google font site.

can you please tell me how i can get the google font link ??

Thanks

xperter avatar Apr 07 '15 07:04 xperter

You can use as example code below:


if(!function_exists('_action_theme_process_google_fonts')) {
    function _action_theme_process_google_fonts()
    {
        $include_from_google = array();
        $google_fonts = fw_get_google_fonts();

        $your_typography_option_1 = fw_get_db_settings_option('your_typography_option_1');
        $your_typography_option_2 = fw_get_db_settings_option('your_typography_option_2');

        // if is google font
        if( isset($google_fonts[$your_typography_option_1['family']]) ){
            $include_from_google[$your_typography_option_1['family']] =   $google_fonts[$your_typography_option_1['family']];
        }

        if( isset($google_fonts[$your_typography_option_1['family']]) ){
            $include_from_google[$your_typography_option_2['family']] =   $google_fonts[$your_typography_option_2['family']];
        }

        $google_fonts_links = fw_theme_get_remote_fonts($include_from_google);
        // set a option in db for save google fonts link
        update_option( 'fw_theme_google_fonts_link', $google_fonts_links );
    }
    add_action('fw_settings_form_saved', '_action_theme_process_google_fonts', 999, 2);
}

if (!function_exists('fw_theme_get_remote_fonts')) :
    function fw_theme_get_remote_fonts($include_from_google) {
        /**
         * Get remote fonts
         * @param array $include_from_google
         */
        if ( ! sizeof( $include_from_google ) ) {
            return '';
        }

        $html = "<link href='http://fonts.googleapis.com/css?family=";

        foreach ( $include_from_google as $font => $styles ) {
            $html .= str_replace( ' ', '+', $font ) . ':' . implode( ',', $styles['variants'] ) . '|';
        }

        $html = substr( $html, 0, - 1 );
        $html .= "' rel='stylesheet' type='text/css'>";

        return $html;
    }
endif;

if (!function_exists('_action_theme_print_google_fonts_link')) :
    function _action_theme_print_google_fonts_link() {
        /**
         * Print google fonts link
         */
        $google_fonts_link = get_option('fw_theme_google_fonts_link', '');
        if($google_fonts_link != ''){
            echo $google_fonts_link;
        }
    }
    add_action('wp_head', '_action_theme_print_google_fonts_link');
endif;

valeriuzdrobau avatar Apr 07 '15 09:04 valeriuzdrobau

Hi @valeriuzdrobau Thanks for this code i have just change this $your_typography_option_1 = fw_get_db_settings_option('your_typography_option_1'); to $your_typography_option_1 = fw_get_db_settings_option('home_title_h1'); and now its showing on soruce code

but its not working on my end. if i click on font link. like http://fonts.googleapis.com/css?family=Tinos:regular,italic,700,700italic%20|: 404 are showing.

is there i am missing something ??

Thanks

xperter avatar Apr 07 '15 10:04 xperter

i got this. if i use only one. this issue happened.

Thanks @valeriuzdrobau

xperter avatar Apr 07 '15 11:04 xperter

@valeriuzdrobau and @moldcraft i have another question. how i echo this value for css [style] => '700italic' some font style value like 700italic here is one for style another for weight, echo will be like font-style: italic; font-weight: 700;

i am not enough good in php :( thats why confused

Thank you

xperter avatar Apr 07 '15 11:04 xperter

            $current_style = $your_typography_option_1['style'];
            if ( $current_style === 'regular' ) {
                $current_style = '400';
            }
            if ( $current_style == 'italic' ) {
                $current_style = '400italic';
            }

            $font_style  = ( strpos( $current_style, 'italic' ) ) ? 'font-style: italic;' : '';
            $font_weight = 'font-weight: ' . intval( $current_style ) . ';';

valeriuzdrobau avatar Apr 07 '15 12:04 valeriuzdrobau

@valeriuzdrobau Thank you so much :100:

xperter avatar Apr 07 '15 17:04 xperter