dh_man / dhead-wp
A collection of classes to manage CPTs, Taxonomies, ACF Blocks, and Options Pages in WordPress.
0.1.1
2025-11-17 07:17 UTC
Requires
- php: >=7.4
README
composer require dh_man/dhead-wp
カスタム投稿タイプとフィールドの例
例えば「お知らせ」投稿タイプ(news)とカスタムフィールドを追加する場合:
use DHead_WP\DH_CPT_Builder; DH_CPT_Builder::make('news') ->args([ 'label' => 'お知らせ', 'public' => true, 'supports' => ['title', 'editor', 'thumbnail'], ]) ->fields([ [ 'name' => 'subtitle', 'label' => 'サブタイトル', 'type' => 'text', ], [ 'name' => 'publish_date', 'label' => '公開日', 'type' => 'date_picker', ], ], 'お知らせ情報');
このコードをテーマやプラグインの初期化時に実行すると、 「news」投稿タイプと「サブタイトル」「公開日」フィールドが追加されます。
オプションページの例
管理画面に「サイト設定」オプションページを追加する場合:
use DHead_WP\DH_Options_Page_Builder; DH_Options_Page_Builder::make('サイト設定') ->menu_title('サイト設定') ->fields([ [ 'name' => 'site_logo', 'label' => 'ロゴ画像', 'type' => 'image', ], [ 'name' => 'contact_email', 'label' => 'お問い合わせメール', 'type' => 'email', ], ]);
このコードを初期化時に実行すると、管理画面に「サイト設定」オプションページが追加され、 ロゴ画像やメールアドレスなどのフィールドを設定できます。
ブロック自動登録の使い方
例えば、resources/views/blocks ディレクトリ内の各ブロックを自動で読み込む場合:
\DHead_WP\Block_Auto_Registrar::path('resources/views/blocks');
ブロックファイルの例
resources/views/blocks/hero/block.php の内容例:
<?php use DHead_WP\DH_Block_Builder; DH_Block_Builder::make('hero') ->title('Hero Block') ->description('ヒーローセクションのブロック') ->icon('cover') ->category('layout') ->fields([ [ 'name' => 'title', 'label' => 'タイトル', 'type' => 'text', ], [ 'name' => 'image', 'label' => '画像', 'type' => 'image', ], ]) ->render_blade('blocks.hero');
Bladeテンプレートファイルの例
上記の ->render_blade('blocks.hero') を使う場合、Sageテーマ等で resources/views/blocks/hero.blade.php を作成します。
例:
{{-- resources/views/blocks/hero.blade.php --}} <section class="hero"> <h1>{{ get_field('title') }}</h1> @php $image = get_field('image'); @endphp @if(!empty($image)) <img src="{{ $image['url'] }}" alt="{{ $image['alt'] }}"> @endif {{-- ACFリピーター例 --}} @if(have_rows('items')) <ul> @while(have_rows('items')) @php the_row(); @endphp <li>{{ get_sub_field('title') }}</li> @endwhile </ul> @endif </section>