Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.