Support

Account

Home Forums Add-ons Options Page Adding submenu pages

Solved

Adding submenu pages

  • I’m trying to set up an admin options menu so that the main menu is the ACF site options menu and under it there is a submenu page which is not generated by ACF (i.e. just uses the standard WP add_submenu_page).

    Is there a way to do that?

    I tried various things for ‘parent_slug’ in add_submenu_page but none of them ended up getting added to the ACF options menu.

    Alternatively, could I add the menu myself (i.e. using WP add_menu_page) and add ACF options as a submenu to that? (I tried but couldn’t get that to work either.)

    Or… if none of those are possible, is there a way to programmatically add my own HTML to an ACF options menu page (so I could add the code I was wanting on a separate page)?

    Thanks.

  • Hi @rowatt

    Perhaps you could post the code you are using?

    Thanks
    E

  • Well… I tried lots of different things. For example, the following got the menus in the right place, but no fields got added to the options page (the only rule option I have is Options Page = Site Options).

    	public function add_site_options()
    	{
    		//add section heading
    		$page_title = 'Site Tools';
    		$menu_title = 'Site Tools';
    		$capability = 'manage_options';
    		$menu_slug = 'site_options_tools';
    		$function = array( $this, 'site_options_tools_html' );
    		$icon_url = NULL;
    		$position = 500;
    		add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    
    		//add general site options menu
    		$parent_slug = 'site_options_tools';
    		$page_title = 'Site Tools';
    		$menu_title = 'Site Tools';
    		$capability = 'manage_options';
    		$menu_slug = 'site_options_tools';
    		$function = array( $this, 'site_options_tools_html' );
    		add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);
    	}
    
    	public function acf_options_setup()
    	{
    		if( function_exists('acf_set_options_page_menu') )
    		{
    			acf_set_options_page_menu( 'Site Options' );
    		}
    
    		if( function_exists('acf_add_options_sub_page') )
    		{
    			acf_add_options_sub_page(array(
    			                              'menu' => 'Site Options',
    			                              'title' => 'Site Options',
    			                              'slug' => 'acf_site_options',
    			                              'parent' => 'site_options_tools',
    			                              'capability' => 'manage_options'
    			                         ));
    		}
    	}
    		//in __construct()
    		$this->add_action( 'plugins_loaded', 'acf_options_setup' );
    		$this->add_action( 'admin_menu', 'add_site_options' );
  • Hi @rowatt

    The ACF plugin will not display any admin pages in the ‘location dropdown’ that have been create via the add_menu_page function. Only admin pages that have been created via the acf_add_options_sub_page function.

    Thanks
    E

  • Thanks Elliot. Sorry if I wasn’t clear… but I wasn’t getting any ACF fields showing on the page I added with “acf_add_options_sub_page”. The name shown in the ‘location dropdown’ is “Site Options” which is the name I gave the sub menu page.

    I’m happy to do it another way altogether if that’s better – the core thing I am trying to achieve is to have an ACF submenu page and a standard WP submenu page under the same menu.

  • Hi @rowatt

    Thanks for the clarification.

    Just to consolodate the info: You are using the acf function acf_add_options_sub_page to successfully add an options page to the wp-admin. This works fine, and you are now attempting to add a normal admin menu item to sit along with it.

    I think the main issue is that your action is running before ACF’s admin_menu. If you look at the code within the options page add-on, you will see that the admin_menu is run with a priority of 11 which is AFTER your default 10.

    Perhaps if you were to increase your priority to 12 or above, you would be able to attach your menu page correctly?

    Thanks
    E

  • Thank you @elliot – it was the ordering of things which caused the problem.

    I wasn’t able to order things right to get an acf options page as a submenu page of a non-ACF menu page, though I suspect it would work fine if I got the order right. I did, however, get my own submenu page as a submenu of the ACF Options menu page – which is what I was aiming for anyway.

    For the benefit of anyone else trying to do this here’s what worked for me:-

    function add_site_options()
    {
    	//add general site options menu
    	$parent_slug = 'acf-options';
    	$page_title = 'Site Tools';
    	$menu_title = 'Site Tools';
    	$capability = 'manage_options';
    	$menu_slug = 'site_options_tools';
    	$function = 'site_options_tools_html';
    	add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);
    }
    add_action( 'admin_menu', 'add_site_options', 12 )
  • Found this issue because I faced the same problem on a site now, with ACF5 Pro. If you’re finding this thread searching around the solution is the same except in ACF5 as you can see below the priority on admin_menu is 99.

    From pro\admin\options-page.php constructor:

    add_action('admin_menu', array($this,'admin_menu'), 99, 0);

    So the solution is use priority 100 or more in your admin_menu call as shown below:

    add_action( 'admin_menu', array( $this, 'adminMenuPages' ), 101);

  • Hi,

    I’m still new to this and I faced the same problem. This is the code I add in function.php

    acf_add_options_sub_page(array(
            'page_title'  => 'Product Settings',
            'menu_title'  => 'Product Settings',
            'menu_slug' => 'product-settings',
            'parent_slug' => 'edit.php?post_type=product',
            'capability'  => 'manage_options',
            'position'  => false,
            'icon_url' => false,
          ));

    I follow the tutorial from here: https://www.bobz.co/dynamically-populate-select-fields-choice-in-advanced-custom-fields/

    When I change to this add_action( 'admin_menu', array( $this, 'adminMenuPages' ), 101) my page went blank.

    Appreciate any advise. Thanks.

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

The topic ‘Adding submenu pages’ is closed to new replies.