Home › Forums › Backend Issues (wp-admin) › How to add ACF fields to my custom admin page
Hi.
I added an option page by add_menu_page function (NOT ACF’s options page feature). I want to add ACF fields to the page.
So I read through the document (https://www.advancedcustomfields.com/resources/custom-location-rules/), then I tried to display whole available admin pages in the value field by using WordPress global variables ($menu and $submenu) but it returns null.
Any help appreciated.
Thank you.
Having read this topic(https://support.advancedcustomfields.com/forums/topic/add-field-group-to-a-custom-plugin-page/) I cannot add ACF fields to custom admin page.
The rule is still applicable now, right?
Hi,
I just had the same problem and found out that you can call the following ACF functions manually, since no ACF location rule can be applied in this case, to render ACF fields in a custom admin page:
1) Load ACF JS (Ref: https://github.com/wp-premium/advanced-custom-fields-pro/blob/master/includes/assets.php#L589)
acf_enqueue_scripts();
2) Render ACF fields (Ref: https://github.com/wp-premium/advanced-custom-fields-pro/blob/master/includes/acf-field-functions.php#L570)
$fields = acf_get_fields('your_group_key');
acf_render_fields($fields);
I hope this helps!
Claudio
Rendering the fields after calling them with acf_get_fields() works but how to use this with data from a custom post type and save changes back to that record?
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.
The topic ‘How to add ACF fields to my custom admin page’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.