Support

Account

Home Forums Add-ons Options Page Add options page to network admin

Solving

Add options page to network admin

  • Is there a way to add an options page to the network admin?

  • Hi @gruvii

    I’m not very familiar with network admin stuff, but the acf_add_options_sub_page (http://www.advancedcustomfields.com/resources/functions/acf_add_options_sub_page/) function does allow for a custom capability setting.

    Perhaps you could use that?

    Thanks
    E

  • To allow the Options page to be shown in Network Admin we need to add it through another hook.

    Options plugin version 1.2.0, line 45:

    Before:
    add_action('admin_menu', array($this,'admin_menu'), 11, 0);

    After

    add_action('admin_menu', array($this,'admin_menu'), 11, 0);
    add_action('network_admin_menu', array($this,'admin_menu'), 11, 0);

    $blog_id resolves to 1 in network admin so all settings will be saved to the first blog. (Usually the network site.)

    Would really appreciate if this could be patched in the next version. This is very useful for multisites.

  • 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.

  • @elliot, I would be a major proponent of this. CF does this quite splendidly (I do a lot of multisite work), but (as a long-time ACF licensee) I’ve been thinking of switching back due to the impressive emphasis being put on PHP-friendliness, and especially, ACF Blocks (which is super exciting!!).

    There are so many great frameworks out there, all with pros and cons, but with all of the multisite work that I do, the Network Admin (sub-)menu options page is something I need. I could run two frameworks, but I prefer to reduce overhead when possible (or do it manually, but ACF and other frameworks are just nicer for such things).

    Cheers, great work and thank you all!
    Daniel

  • I just wrote a plugin to solve some of this without hacking the core. Please see:

    https://github.com/owlwatch/acf-multisite-options

    This allows you to create network options pages with the same ACF API, you just need to add a network attribute to the options when using acf_add_options_page or acf_add_options_sub_page. For example:

    
    acf_add_options_page([
        'network' => true,
        'post_id' => 'acf_network_options',
        'page_title' => 'Network Options',
        'menu_title' => 'Network Options'
    ]);
    

    The values are stored in the ‘sitemeta’ table, so they are accessible across sites without needing to use the switch_to_blog function.


    @elliot
    – There is a lot of extra code that could be reduced if there were a filters in place in the acf_get_metadata, acf_update_metadata and acf_delete_metadata functions. Alternatively, this functionality could be built into the core fairly easily. If you are interested, I could create a pull request, but I would need to do so in the Pro repo.

    Hope this helps some people. I am using in a production site now and will make this plugin available on the WordPress directory once I’ve tested a bit more.

  • I just wrote a plugin to solve some of this without hacking the core. Please see:

    https://github.com/owlwatch/acf-multisite-options

    This allows you to create network options pages with the same ACF API, you just need to add a network attribute to the options when using acf_add_options_page or acf_add_options_sub_page. For example:

    
    acf_add_options_page([
        'network' => true,
        'post_id' => 'acf_network_options',
        'page_title' => 'Network Options',
        'menu_title' => 'Network Options'
    ]);
    

    I’m not sure what happened to my reply on this thread, so pardon me if this is a duplicate post.

    The values are stored in the ‘sitemeta’ table, so they are accessible across sites without needing to use the switch_to_blog function.


    @elliot
    – There is a lot of extra code that could be reduced if there were a filters in place in the acf_get_metadata, acf_update_metadata and acf_delete_metadata functions. Alternatively, this functionality could be built into the core fairly easily. If you are interested, I could create a pull request, but I would need to do so in the Pro repo.

    Hope this helps some people. I am using in a production site now and will make this plugin available on the WordPress directory once I’ve tested a bit more.

  • @fabrizim I would submit your suggestion directly to Eliot here https://www.advancedcustomfields.com/contact/. I don’t know if he’s still following this topic and most of his time right now is being consumed by gooberbug so contacting is the best way to get his attention.

  • Hi @fabrizim

    Did you pull request this in the acf plugin or made your plugin available on the WP directory?

    How should I setup some ACF field group to be visible only in given network options page?

    Thanks

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Add options page to network admin’ is closed to new replies.