wptt/webfont-loader
Composer 安装命令:
composer require wptt/webfont-loader
包简介
Locally host webfonts.
关键字:
README 文档
README
Downloads webfonts (like for example Google-Fonts), and hosts them locally on a WordPress site.
This improves performance (fewer requests to multiple top-level domains) and increases privacy. Since fonts get hosted locally on the site, there are no pings to a 3rd-party server to get the webfonts and therefore no tracking.
Usage
A WordPress theme will typically enqueue assets using the wp_enqueue_style function:
function my_theme_enqueue_assets() { // Load the theme stylesheet. wp_enqueue_style( 'my-theme', get_stylesheet_directory_uri() . '/style.css', array(), '1.0' ); // Load the webfont. wp_enqueue_style( 'literata', 'https://fonts.googleapis.com/css2?family=Literata&display=swap', array(), '1.0' ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
To locally host the webfonts, you will first need to download the wptt-webfont-loader.php file from this repository and copy it in your theme. Once you do that, the above code can be converted to this:
function my_theme_enqueue_assets() { // Include the file. require_once get_theme_file_path( 'inc/wptt-webfont-loader.php' ); // Load the theme stylesheet. wp_enqueue_style( 'my-theme', get_stylesheet_directory_uri() . '/style.css', array(), '1.0' ); // Load the webfont. wp_enqueue_style( 'literata', wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Literata&display=swap' ), array(), '1.0' ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
Available functions
wptt_get_webfont_styles
$remote_url = 'https://fonts.googleapis.com/css2?family=Literata&display=swap';
$contents = wptt_get_webfont_styles( $remote_url );
Returns the stylesheet contents, using locally hosted webfonts.
wptt_get_webfont_url
$remote_url = 'https://fonts.googleapis.com/css2?family=Literata&display=swap';
$contents = wptt_get_webfont_url( $remote_url );
Returns a stylesheet URL, locally-hosted.
Build url for multiple fonts
$font_families = array( 'Quicksand:wght@300;400;500;600;700', 'Work+Sans:wght@300;400;500;600;700' ); $fonts_url = add_query_arg( array( 'family' => implode( '&family=', $font_families ), 'display' => 'swap', ), 'https://fonts.googleapis.com/css2' ); $contents = wptt_get_webfont_url( esc_url_raw( $fonts_url ) );
Supporting IE
The wptt_get_webfont_url will - by default - download .woff2 files. However, if you need to support IE you will need to use .woff files instead. To do that, you can pass woff as the 2nd argument in the wptt_get_webfont_url function:
wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Literata&display=swap', 'woff' );
Storing In A Custom Directory
If you have the need to store font files in a custom directory you can pass a custom path and URL using filters. Be sure you add these filters BEFORE the file containing the WPTT_WebFont_Loader class is called.
/** * Change the base path. * This is by default WP_CONTENT_DIR. * * NOTE: Do not include trailing slash. */ add_filter( 'wptt_get_local_fonts_base_path', function( $path ) { return WP_CONTENT_DIR; } ); /** * Change the base URL. * This is by default the content_url(). * * NOTE: Do not include trailing slash. */ add_filter( 'wptt_get_local_fonts_base_url', function( $url ) { return content_url(); } ); /** * Change the subfolder name. * This is by default "fonts". * * Return empty string or false to not use a subfolder. */ add_filter( 'wptt_get_local_fonts_subfolder_name', function( $subfolder_name ) { return 'fonts'; } );
wptt/webfont-loader 适用场景与选型建议
wptt/webfont-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56.73k 次下载、GitHub Stars 达 126, 最近一次更新时间为 2020 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wptt/webfont-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wptt/webfont-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wptt/webfont-loader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Inertia.js server-side adapter for PHP. Handles full visits, Inertia XHR visits, asset-version 409 handshakes and partial reloads. Framework-agnostic, with transparent WordPress integration.
Adds Cache-Tag HTTP response headers for singular WordPress content and purges Cloudflare by tag when content changes.
AI Chatbot & Helpdesk Agent for WooCommerce. Connects your store to Egentify for AI-powered chat, order tracking, email, and ticketing.
MIDDAG's reference architecture for a WordPress plugin — composes the OSS framework/wordpress/ui libraries end to end.
Assets helper package for the Hybrid Core framework.
统计信息
- 总下载量: 56.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 126
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-29