Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_submenu_page position / order not working #1380

Closed
coolsatinder opened this issue Oct 22, 2020 · 11 comments
Closed

add_submenu_page position / order not working #1380

coolsatinder opened this issue Oct 22, 2020 · 11 comments

Comments

@coolsatinder
Copy link

There is no option to set position or order a submenu item inside an already exist main menu item.

Currently It is possible via small adjustments:-

  1. Add $this->cmb->prop( 'position' ) inside add_submenu_page() inside CMB2\includes\CMB2_Options_Hookup.php
  2. Also add an extra property - priority that we can use inside menu add_action hook. add_action( $hook, array( $this, 'options_page_menu_hooks' ), $this->cmb->prop( 'priority' ) );

Hope next version will add some adjustments to order a submenu.

@tw2113
Copy link
Contributor

tw2113 commented Oct 22, 2020

Looks like a good potential new enhancement, thanks for pointing this out.

For those maybe not familiar, the position argument looks like it was added in WordPress 5.3

@coolsatinder
Copy link
Author

Can I do this without changing inside CMB2 folder inside my plugin now?

It works by changing inside CMB2 but in that way other plugin that also includes CMB2 can again over-ride this.

@tw2113
Copy link
Contributor

tw2113 commented Oct 23, 2020

Unsure how easy it'd be to extend this part of CMB2 to use your own version instead of the version in CMB2 itself for all the appropriate arguments etc.

I do know that CMB2 has some pretty smart loading going on where the latest version will get loaded first and any other version found in the website doesn't get used.

Beyond that, the only way I can think of would be just modifying or forking your own version to use for the time being, until someone decides to actively work on it and get it into the develop branch and/or release.

@james0r
Copy link

james0r commented Dec 2, 2020

I'm not sure how close of a request this is to what you're asking but I'm trying to make the first submenu item of the options-page a different name than it's parent item.

Screen Shot 2020-12-02 at 7 28 44 PM

See the standard Wordpress settings menu item and its children for an example.

@alfonmaco
Copy link

alfonmaco commented Dec 3, 2020

Hello james0r, to change the name of the submenu itme, you must change the third parameter of the add_submenu_page() function.

add_menu_page(										
	'Practice Info',				//'page_title'
	'Practice Info',				//'menu_title'
	'manage_options', 			//'capability'
	'practice-info', 				//'menu-slug'
	'practice_info',				//'function'
	'',						//'icono'											
							//'position'
);
add_submenu_page(
	'practice-info',				//'parent_slug'
	'Practice info config',			//'page_title'
	'submenu name',			//'menu_title'
	'manage_options', 			//'capability'
	'practice-config-page', 		//'menu-slug'
	'practice_config_function',		//'function'
								//'position'
);

@james0r
Copy link

james0r commented Dec 3, 2020

That's only if I was directly using the Wordpress function correct?

What about when creating theme options with CMB2? It seems to automatically create this first submenu item and I don't see where I have control over its naming. Maybe there's some perfect combination of arguments I'm supposed two pass in my metabox array but I havn't found it yet.

$args = [
    'id'              => 'cmb2_id_box_theme_options',
    'object_types'    => ['options-page'],
    'option_key'      => 'cmb_main_options',
    'menu_title'      => __('Keepers', 'keepers'),
    'position'        => 2,
    'capability'      => 'manage_options', // Cap required to view options-page.
];

$cmb2_options_clinic_info = new_cmb2_box($args);

that's how I create my metabox for the parent item.

Same thing occurs with this snippet from the snippet library -> https://github.com/CMB2/CMB2-Snippet-Library/blob/master/options-and-settings-pages/theme-options-cmb.php

@tw2113
Copy link
Contributor

tw2113 commented Dec 3, 2020

Does remind me and make me re-realize that this is something we'd need to get support added for to CMB2 still, as it's not at the moment.

Anyone wanting to submit pull requests would be most welcome as well.

@alfonmaco
Copy link

alfonmaco commented Dec 3, 2020

keepers
tray this, you must set capability of menu to 'unknown', and add submenus, when you press on the menu tag is the same as press the first submenu.


$args = [
    'id'              => 'cmb2_id_box_theme_options',
    'object_types'    => ['options-page'],
    'option_key'      => 'cmb_main_options',
    'menu_title'      => __('Keepers', 'keepers'),
    'position'        => 2,
    'capability'      => 'unknown' //'manage_options', // Cap required to view options-page.
];

$cmb2_options_clinic_info = new_cmb2_box($args);
$args = [
    'id'              => 'cmb2_id_box_theme_options_1',
    'object_types'    => ['options-page'],
    'option_key'      => 'cmb_main_options1',
    'menu_title'      => __('Keepers1', 'keepers1'),
    'title'						=> __('Keepers1', 'keepers1'),
    'parent_slug'     => 'cmb_main_options',
    'position'        => 2,
    'capability'      => 'manage_options', // Cap required to view options-page.
];

$cmb2_options_clinic_info = new_cmb2_box($args);
$args = [
    'id'              => 'cmb2_id_box_theme_options_2',
    'object_types'    => ['options-page'],
    'option_key'      => 'cmb_main_options2',
    'menu_title'      => __('Keepers2', 'keepers2'),
    'title'						=> __('Keepers2', 'keepers2'),
    'parent_slug'     => 'cmb_main_options',
    'position'        => 2,
    'capability'      => 'manage_options', // Cap required to view options-page.
];

$cmb2_options_clinic_info = new_cmb2_box($args);

@alfonmaco
Copy link

alfonmaco commented Dec 4, 2020

For the solution maybe if you change this function in the CMB2_Options_Hookup.php file, the order work fine. I dont know, how can i propose the change, because i do not know enought about github.

`.

public function hooks() {
	if ( empty( $this->option_key ) ) {
		return;
	}

	if ( ! $this->cmb->prop( 'autoload', true ) ) {
		// Disable option autoload if requested.
		add_filter( "cmb2_should_autoload_{$this->option_key}", '__return_false' );
	}

	/**
	 * For WP < 4.7. Ensure the register_setting function exists.
	 */
	if ( ! CMB2_Utils::wp_at_least( '4.7' ) && ! function_exists( 'register_setting' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	// Register setting to cmb2 group.
	register_setting( 'cmb2', $this->option_key );

	// Handle saving the data.
	add_action( 'admin_post_' . $this->option_key, array( $this, 'save_options' ) );

	// Optionally network_admin_menu.
	$hook = $this->cmb->prop( 'admin_menu_hook' );
	
	/*
	*		changes to get working sub_menu position
	*
	*/
	
	
	$parent_slug = $this->cmb->prop( 'parent_slug' );
	$position    = $this->cmb->prop( 'position' );

	if ( $parent_slug ) { 				
		// Hook in to add our sub_menu.
		if ( $position > 0 ){
			// Hook in to add our sub_menu setting position
			add_action( $hook, array( $this, 'options_page_menu_hooks' ), 100 * $position );
		}
		else { 				
			// Hook in to add our sub_menu in a very last position
			add_action( $hook, array( $this, 'options_page_menu_hooks' ), 2000 );
		}
	}
	else{
		// Hook in to add our menu.
		add_action( $hook, array( $this, 'options_page_menu_hooks' ) );
	}

	/*
	*	end of changes
	*
	*/

	// If in the network admin, need to use get/update_site_option.
	if ( 'network_admin_menu' === $hook ) {
		// Override CMB's getter.
		add_filter( "cmb2_override_option_get_{$this->option_key}", array( $this, 'network_get_override' ), 10, 2 );
		// Override CMB's setter.
		add_filter( "cmb2_override_option_save_{$this->option_key}", array( $this, 'network_update_override' ), 10, 2 );
	}
}

`

@james0r
Copy link

james0r commented Dec 5, 2020

@alfonmaco Great stuff! Yes, changing the cap to 'unknown' did the trick for me. Almost seems more intuitive IMHO for this to be the default behavior with options-page boxes.

I'm not having any issues with the position yet but if I do i'll check out your findings here.

Really appreciate it.

@jtsternberg
Copy link
Member

The priority box property is now used for the hook priority for the add_action to register the option pages:
82c3fc8...develop#diff-7a27804f02c9964249e8fcdfc649140623a59dc2ea8548ad124d35021969dc99

lipemat added a commit to lipemat/CMB2 that referenced this issue Mar 30, 2021
* upstream/develop:
  Clean up and add props for CMB2#1413
  Sanitize URLs with HTTPS
  Add develop suffix to init class
  Add am-cli-tools
  Update changelong and version numbers and readmes, and prepare release
  Set default priority to 10 for options pages. Fixes CMB2#1410
  build field-cache key manually to remove unnecessary |'s
  Better generated array key for cached fields, fixes issue where wrong field is found. Fixes CMB2#1405
  Add to list of valid image types from get_allowed_mime_types(). Fixes CMB2#1223
  Move tab markup output to separate method, options_page_tab_nav_output. Fixes CMB2#1407
  Add cmb2_tab_group_tabs filter for adding arbitrary menu page urls to the cmb2 tabs. See CMB2#1407
  Update since tag, and add props for CMB2#1340
  Limit use of italic, including removing from field descriptions. Fixes CMB2#1404
  Add props for CMB2#1400
  move $args in deprecated_param method for 7.4
  Add develop suffix to init class
  Prepare release and changelog for 2.8.0
  Fix tests since WP_Error signature changed
  move $args in deprecated_param method for 7.4
  Use the already-existing get_priority method. Re CMB2#1380 and CMB2#1398
  Use existing "priority" field param. Fixes CMB2#1380. Closes CMB2#1398
  Add admin_menu_hook_priority box property for options boxes. Fixes CMB2#1380. Closes CMB2#1398
  Make field_can first param required to address php 8 "Required parameter follows optional parameter". Fixes CMB2#1396
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/CMB2_Utils.php
  Prevent array to string conversion
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Added sanitize_color() function and remove PHP warnings suppresions
  Fixes PHP warnings on repeatable ColorPicker with an array as default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants