Home › Forums › Backend Issues (wp-admin) › How to add ACF fields to my custom admin page › Reply To: How to add ACF fields to my custom admin page
Hello.
I ran into the same problem, but I was able to do it with this code.
function.php
<?php
function my_page_settings_page() {
acf_form([
'post_id' => 'options', // オプションページの場合は 'options' を指定
'field_groups' => ['group_〇〇〇〇'], // 表示するフィールドグループのID
'submit_value' => 'save', // 保存ボタンのテキスト
]);
}
function my_add_settings_page() {
add_menu_page(
'test', // メニュータイトル
'test', // ページタイトル
'manage_options', // アクセス権限(この例では「manage_options」を指定)
'page-settings', // ページスラッグ(一意の識別子)
'my_page_settings_page', // 表示コールバック関数
'dashicons-admin-generic', // アイコン(任意)
2 // メニューの位置
);
}
add_action( 'admin_menu', 'my_add_settings_page' );
function my_admin_head() {
if (isset($_GET['page']) && $_GET['page'] === 'page-settings') {
acf_form_head();
}
}
add_action('admin_init', 'my_admin_head');
Include acf_form_head in “admin_init” because “Warning: Cannot modify header information – headers already sent by” error occurs when saving if it is included in my_page_settings_page.
I am Japanese, so my English may be strange.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.