php Google字体定制工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Google字体定制工具相关的知识,希望对你有一定的参考价值。

$headings_font = esc_html( get_theme_mod( 'custom_headings_font' ) );
$body_font     = esc_html( get_theme_mod( 'custom_body_font' ) );

if ( $headings_font ) {
    wp_enqueue_style( 'custom-headings-fonts', '//fonts.googleapis.com/css?family=' . $headings_font );
} else {
    wp_enqueue_style( 'custom-source-sans', '//fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,700' );
}

if ( $body_font ) {
    wp_enqueue_style( 'custom-body-fonts', '//fonts.googleapis.com/css?family=' . $body_font );
} else {
    wp_enqueue_style( 'custom-source-body', '//fonts.googleapis.com/css?family=Source+Sans+Pro:400,400italic,600,700' );
}

// Adds helper functions.
require_once get_stylesheet_directory() . '/lib/helper-functions.php';
<?php

return array(
    'Source Sans Pro:400,700,400i,700i'            => 'Default',
    'Source Sans Pro:400,700,400italic,700italic'  => 'Source Sans Pro',
    'Open Sans:400italic,700italic,400,700'        => 'Open Sans',
    'Oswald:400,700'                               => 'Oswald',
    'Playfair Display:400,700,400italic'           => 'Playfair Display',
    'Montserrat:400,700'                           => 'Montserrat',
    'Raleway:400,700'                              => 'Raleway',
    'Droid Sans:400,700'                           => 'Droid Sans',
    'Lato:400,700,400italic,700italic'             => 'Lato',
    'Arvo:400,700,400italic,700italic'             => 'Arvo',
    'Lora:400,700,400italic,700italic'             => 'Lora',
    'Merriweather:400,300italic,300,400italic,700,700italic' => 'Merriweather',
    'Oxygen:400,300,700'                           => 'Oxygen',
    'PT Serif:400,700'                             => 'PT Serif',
    'PT Sans:400,700,400italic,700italic'          => 'PT Sans',
    'PT Sans Narrow:400,700'                       => 'PT Sans Narrow',
    'Cabin:400,700,400italic'                      => 'Cabin',
    'Fjalla One:400'                               => 'Fjalla One',
    'Francois One:400'                             => 'Francois One',
    'Josefin Sans:400,300,600,700'                 => 'Josefin Sans',
    'Libre Baskerville:400,400italic,700'          => 'Libre Baskerville',
    'Arimo:400,700,400italic,700italic'            => 'Arimo',
    'Ubuntu:400,700,400italic,700italic'           => 'Ubuntu',
    'Bitter:400,700,400italic'                     => 'Bitter',
    'Droid Serif:400,700,400italic,700italic'      => 'Droid Serif',
    'Roboto:400,400italic,700,700italic'           => 'Roboto',
    'Open Sans Condensed:700,300italic,300'        => 'Open Sans Condensed',
    'Roboto Condensed:400italic,700italic,400,700' => 'Roboto Condensed',
    'Roboto Slab:400,700'                          => 'Roboto Slab',
    'Yanone Kaffeesatz:400,700'                    => 'Yanone Kaffeesatz',
    'Rokkitt:400'                                  => 'Rokkitt',
);
/*
 * Props to the BLDR Theme: https://wordpress.org/themes/bldr/
 */

// Sanitizes Fonts.
function custom_sanitize_fonts( $input ) {
    global $custom_google_fonts;

    if ( array_key_exists( $input, $custom_google_fonts ) ) {
        return $input;
    } else {
        return '';
    }
}
// Defaults
function social_good_customizer_get_default_custom_site_title_font() {
	return 'Montserrat:400,700';
}
$custom_google_fonts = require_once __DIR__ . '/google-fonts.php';
add_action( 'customize_register', 'social_good_customizer_register' );

//-----------------------Typography Section -----------------------//
global $custom_google_fonts;
    
    $wp_customize->add_section(
        'custom_google_fonts_section', array(
            'title'    => __( 'Typography', 'social good' ),
            'description' => __('Customize the site typography here. Select your font from the drop down menu. Fonts provided by Google.', 'social good'),
            'priority' => 41,
        )
    );

    $wp_customize->add_setting(
        'custom_site_title_font', array(
            'default' =>' social_good_customizer_get_default_custom_site_title_font()',
            'sanitize_callback' => 'custom_sanitize_fonts',
        )
    );

    $wp_customize->add_control(
        'custom_site_title_font', array(
            'type'        => 'select',
            'description' => __( 'Select your font for the title of your site.', 'social good' ),
            'label'    => __( 'Site Title', 'social good' ),
            'section'     => 'custom_google_fonts_section',
            'choices'     => $custom_google_fonts,
        )
    );

     $wp_customize->add_setting(
        'custom_navigation_font', array(
            'default' => social_good_customizer_get_default_custom_navigation_font(),
            'sanitize_callback' => 'custom_sanitize_fonts',
        )
    );

    $wp_customize->add_control(
        'custom_navigation_font', array(
            'type'        => 'select',
            'description' => __( 'Select your font for the navigation.', 'social good' ),
            'label'    => __( 'Navigation', 'social good' ),
            'section'     => 'custom_google_fonts_section',
            'choices'     => $custom_google_fonts,
        )
    );
 
// Fonts.
$headings_font = esc_html( get_theme_mod( 'custom_headings_font' ) );
$body_font     = esc_html( get_theme_mod( 'custom_body_font' ) );

    if ( $site_font ) {
    $font_pieces = explode( ':', $site_font );

    $css .= ".site-title a, .site-title a:focus, .site-title a:hover { font-family: {$font_pieces[0]}; }";
    }
    
    if ( $nav_font ) {
    $font_pieces = explode( ':', $nav_font );

    $css .= ".genesis-nav-menu, .nav-primary { font-family: {$font_pieces[0]}; }";
    }

以上是关于php Google字体定制工具的主要内容,如果未能解决你的问题,请参考以下文章

php 通过functions.php将Google字体添加到WP

php 加载Google字体。

php 包含Google字体代码删除功能。

php 包含Google字体代码删除功能。

php 将Google字体注册到WP

php 删除Enfold Google Web字体