Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active October 5, 2023 06:39
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save RadGH/a1473a24782e93435951ef0f390deb2e to your computer and use it in GitHub Desktop.
Save RadGH/a1473a24782e93435951ef0f390deb2e to your computer and use it in GitHub Desktop.
ACF: Display custom metabox on an ACF (sub-) options page
<?php
/**
* Add sub options page with a custom post id
*/
if( function_exists('acf_add_options_page') ) {
acf_add_options_sub_page(array(
'page_title' => 'CSV Sync',
'menu_title' => 'CSV Sync',
'parent_slug' => 'users.php',
'post_id' => 'aa_ucs',
'autoload' => false,
));
}
/**
* Registers a metabox with ACF for a particular screen. You may need to find the screen ID yourself.
*/
function aa_ucs_register_acf_metabox() {
// Verify the screen ID
if ( !acf_is_screen( 'users_page_acf-options-csv-sync' ) ) return;
// Add meta box
add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), 'aa_ucs_display_acf_metabox', 'acf_options_page', 'normal' );
}
add_action( 'acf/input/admin_head', 'aa_ucs_register_acf_metabox', 10 );
/**
* Display custom metabox on an ACF options page
*/
function aa_ucs_display_acf_metabox() {
$text = get_option( 'example-text' );
?>
<input type="text" name="exampletext" placeholder="Enter some example text to be saved" value="<?php echo esc_attr($text); ?>">
<?php
}
/**
* Submit metabox form, save the results
*
* @param $post_id
*/
function aa_ucs_save_acf_metabox_fields( $post_id ) {
if ( $post_id != 'aa_ucs' ) return;
$text = isset($_POST['exampletext']) ? stripslashes($_POST['exampletext']) : false;
update_option( 'example-text', $text );
}
add_action( 'acf/save_post', 'aa_ucs_save_acf_metabox_fields', 20 );
@alanmcginnis
Copy link

Thanks for this. I spent at least an hour trying to put together the right Google search and finally ended up here which is exactly what I needed.

@RadGH
Copy link
Author

RadGH commented Oct 15, 2019

I'm glad it helped you out! I had the same trouble finding a solution and pieced this together based on how the ACF source code creates meta boxes. As far as I know this is the best "ACF way" to do this. Let me know if you have any suggestions for it :)

@etomProductions
Copy link

What can I say less than PERFECT.
Thank you so much for saving our times in (THE RIGHT WAY).

One of the thing I hate in any paid extensions that the creators or what ever we can name them mean to make you get lost when you need to do the thing in the right way.

I saw your comment in ACF blog too, and the wear thing that they never reply to you in the same time they replied to my email in something else also with the wrong answer.

Thank you so much @RadGH.

@RadGH
Copy link
Author

RadGH commented Nov 12, 2020

@etomProductions Thanks, I really appreciate that! I'm glad you found this helpful 🙂

@josephfusco
Copy link

Thanks for this example @RadGH!

For anyone else who may be stuck with acf/save_post not firing, you need at least 1 real ACF field attached to the options page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment