概要
elementor/fonts/groupsフィルタは、WordPressのページビルダープラグインElementorにおいて、フォントグループをカスタマイズするために使用されます。このフィルタを使用することで、デフォルトのフォント設定を変更したり、新たなフォントグループを追加したりできます。
具体的には、以下のような機能実装時によく使われるフィルタです:
- カスタムフォントの追加
- 特定のページや投稿タイプに応じたフォント設定の調整
- プロジェクトのブランドガイドラインに基づいたフォントの統一
- フォント選択UIの拡張
- 自前のフォントライブラリの統合
- フォントのプレビュー機能の改善
構文
add_filter('elementor/fonts/groups', 'custom_font_groups');
パラメータ
$font_groups: フォントグループの配列
戻り値
- カスタマイズされたフォントグループの配列
プラグインとWordPressのバージョン
- Elementorのバージョン: 3.0以上
- WordPressのバージョン: 5.0以上
サンプルコード
サンプルコード1
add_filter('elementor/fonts/groups', function($font_groups) {
$font_groups['custom_fonts'] = [
'label' => __('Custom Fonts', 'text-domain'),
'fonts' => [
'font1' => __('Font One', 'text-domain'),
'font2' => __('Font Two', 'text-domain'),
],
];
return $font_groups;
});
このコードは、custom_fontsという新しいフォントグループを作成し、2つのカスタムフォントを追加します。
サンプルコード2
add_filter('elementor/fonts/groups', function($font_groups) {
if (isset($font_groups['google_fonts'])) {
unset($font_groups['google_fonts']);
}
return $font_groups;
});
このコードは、デフォルトのGoogleフォントグループを削除します。
サンプルコード3
add_filter('elementor/fonts/groups', function($font_groups) {
$font_groups[] = ['label' => 'My Brand Fonts', 'fonts' => ['font3', 'font4']];
return $font_groups;
});
このコードは、My Brand Fontsという新しいラベルを持つフォントグループを追加します。
サンプルコード4
add_filter('elementor/fonts/groups', function($font_groups) {
$font_groups['system_fonts'] = [
'label' => __('System Fonts', 'text-domain'),
'fonts' => ['Arial', 'Verdana', 'Helvetica'],
];
return $font_groups;
});
このコードは、システムフォントを含む新しいフォントグループを追加します。
サンプルコード5
add_filter('elementor/fonts/groups', function($font_groups) {
if (!isset($font_groups['standard_fonts'])) {
$font_groups['standard_fonts'] = [
'label' => __('Standard Fonts', 'text-domain'),
'fonts' => ['Times New Roman', 'Georgia'],
];
}
return $font_groups;
});
このコードは、standard_fontsという新しいフォントグループを作成し、標準フォントをセットします。まだ存在しない場合のみ追加されます。
この関数のアクションでの使用可能性
| アクション名 | 使用可能 |
|---|---|
| mu_plugin_loaded | |
| registered_post_type | |
| plugins_loaded | |
| wp_roles_init | |
| setup_theme | |
| after_setup_theme | |
| set_current_user | |
| init | |
| register_sidebar | |
| wp_loaded | |
| send_headers | |
| parse_query | |
| pre_get_posts | |
| wp | |
| template_redirect | |
| get_header | |
| wp_head |