acf_register_admin_tool()


WordPressのacf_register_admin_tool()関数は、ACF(Advanced Custom Fields)プラグインでカスタム管理ツールを登録するために使用されます。この関数を使うことで、ACFの管理画面に新しいツールを追加できます。

構文

acf_register_admin_tool( string $tool_class );
  • $tool_class (string) — 登録するツールのクラス名を指定します。このクラスはACF_Admin_Toolを継承している必要があります。

例1: カスタムツールの登録

以下は、カスタムツールを登録する基本的な例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { echo '<p>これはカスタムツールです。</p>'; } } acf_register_admin_tool('My_Custom_Tool');

例2: ツールにアクションを追加

ツールにフォームやボタンを追加し、アクションを実行する例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { echo '<form method="post"><input type="submit" name="my_action" value="実行"></form>'; if (isset($_POST['my_action'])) { $this->handle_action(); } } private function handle_action() { echo '<p>アクションが実行されました。</p>'; } } acf_register_admin_tool('My_Custom_Tool');

例3: ツールにCSSを追加

ツールの見た目をカスタマイズするためにCSSを追加する例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { echo '<style>.my-tool { color: red; }</style><div class="my-tool">これは赤いテキストです。</div>'; } } acf_register_admin_tool('My_Custom_Tool');

例4: ツールにJavaScriptを追加

ツールにインタラクティブな機能を追加するためにJavaScriptを追加する例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { echo '<script>document.addEventListener("DOMContentLoaded", function() { alert("ツールが読み込まれました"); });</script><div>JavaScriptが動作します。</div>'; } } acf_register_admin_tool('My_Custom_Tool');

例5: ツールに権限チェックを追加

特定のユーザーのみがツールを使用できるように権限チェックを追加する例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { if (!current_user_can('manage_options')) { echo '<p>権限がありません。</p>'; return; } echo '<p>管理者のみがこのツールを使用できます。</p>'; } } acf_register_admin_tool('My_Custom_Tool');

例6: ツールにデータベース操作を追加

ツールを使用してデータベースを操作する例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { if (isset($_POST['update_db'])) { global $wpdb; $wpdb->update($wpdb->posts, array('post_status' => 'draft'), array('post_status' => 'publish')); echo '<p>データベースが更新されました。</p>'; } echo '<form method="post"><input type="submit" name="update_db" value="データベースを更新"></form>'; } } acf_register_admin_tool('My_Custom_Tool');

例7: ツールにリダイレクトを追加

ツールのアクション後にリダイレクトを行う例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { if (isset($_POST['redirect'])) { wp_redirect(admin_url()); exit; } echo '<form method="post"><input type="submit" name="redirect" value="リダイレクト"></form>'; } } acf_register_admin_tool('My_Custom_Tool');

例8: ツールにカスタムフィールドを表示

ツール内でカスタムフィールドを表示する例です。

class My_Custom_Tool extends ACF_Admin_Tool { public function initialize() { $this->name = 'my_custom_tool'; $this->title = __('マイカスタムツール', 'acf'); } public function html() { $field_value = get_field('my_custom_field', get_the_ID()); echo '<p>カスタムフィールドの値: ' . esc_html($field_value) . '</p>'; } } acf_register_admin_tool('My_Custom_Tool');

注意点

  • acf_register_admin_tool()を使用する際は、必ずACF_Admin_Toolクラスを継承したクラスを作成してください。
  • ツールの機能を実装する際は、セキュリティに注意し、適切な権限チェックを行ってください。
  • ツールのHTML出力は、エスケープ処理を行い、XSS攻撃を防ぐようにしてください。