Skip to content

Front End Font Loader #3

@darknailblue

Description

@darknailblue

Hi! I'm making a theme that uses the typography field and found that I needed to write a simple font-end compiler

Below is a sample function that grabs all unique font families from all options pages and then calls them on the front end.

function anf_load_custom_fonts ( ) {
    $fields = get_field_objects('option');      // or whatever custom post, id, etc.
    $font_scraper = array();

    // Grab each unique instance of a custom font
    foreach ($fields as $key => $value) {
        if ( $value['type'] == 'typography' ) {
            $family = $value['value']['font-family'];
            if ( !array_search($family, $font_scraper))
                $font_scraper[$key] = $family;
        }
    }

    // create a string
    foreach ($font_scraper as $font) {
        $fonts = implode("|", $font_scraper);
    }

    // enqueue the necessary fonts
    if ( $fonts ) {
        wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family=' . $fonts, array(), NULL, false );
    }
}

Just wondering if there is a way that similar code could be introduced into the plugin. Would be very useful!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions