Home › Forums › Add-ons › Options Page › Add options page to network admin › Reply To: Add options page to network admin
I’ve found potentially a better way to add menus to the network admin menu. Rather than just adding actions for both admin_menu and network_admin_menu, let’s add a conditional check in the acf admin_menu function so that we’re not adding pages to both menus.
after you’ve added the action on line 25 as stated above, thanks @snitchhomer.
update admin_menu function on /advanced-custom-fields-pro/pro/admin/options-page.php line 161
function admin_menu() {
// vars
$pages = acf_get_options_pages();
// create pages
if( !empty($pages) ) {
foreach( $pages as $page ) {
// vars
$slug = '';
if ( current_filter() === 'admin_menu' ) {
if( empty($page['parent_slug']) ) {
// add page
$slug = add_menu_page( $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array($this, 'html'), $page['icon_url'], $page['position'] );
} else {
// add page
$slug = add_submenu_page( $page['parent_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array($this, 'html') );
}
} elseif ( current_filter() === 'network_admin_menu' ) {
if ( $page['parent_slug'] === 'network/' ) { //if parent_slug is 'network/'
//add network menu page - remove network from the parent_slug though
$slug = add_menu_page( $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array($this, 'html'), $page['icon_url'], $page['position'] );
} elseif ( substr( $page['parent_slug'], 0, 8 ) === 'network/' ) { //if parent_slug starts with 'network/'
// add page
$slug = add_submenu_page( substr( $page['parent_slug'], 8), $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array($this, 'html') );
}
}
// actions
add_action("load-{$slug}", array($this,'admin_load'));
}
}
}
This will check if the parent slug includes ‘network/’ so we can use the same idea of just specifying the parent slug and the plugin will properly place the menu in the right spot, since the network menu always starts with ‘network/’. If it’s exactly ‘network/’ then it creates a page, and if it starts with ‘network/’ it creates a sub page, and if it’s not one of this it doesn’t belong in the network menu anyways so it’s ignored. We’re also checking if the filter came from ‘admin_menu’ or ‘network_admin_menu’ and only firing off the corresponding add_menu calls in each case.
I haven’t done extensive testing, but so far it’s working great for me. @elliot, let me know if there’s a way to submit a patch or pull request. I’d be happy to contribute.
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.