Skip to content

Conversation

@alfonmaco
Copy link

With this changes, the order of the menu options work fine.

Description

If position is indicated, we assign the indicated position multiplied by 100, and if it is not indicated we assign the value 2000 that places it at the end of the list.

Motivation and Context

With this changes, the order of the menu options work fine.

Fixes #1380.

Risk Level

Testing procedure

I create diferent menus, and change the position value, and see the position is set correct.
I think this changes do not affect aother areas of the code.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

Screenshots

With this changes, the order of the menu options work fine.
@jtsternberg
Copy link
Member

You can now explicitly define the hook priority via the admin_menu_hook_priority box property.

@jtsternberg
Copy link
Member

Changed my mind. Will use the already existing priority box property.

@alfonmaco
Copy link
Author

alfonmaco commented Jan 5, 2021

With my commit, you can sort the submenu options inside a menu option.

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

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

	$group_field_id = $post_box->add_field(
		[
			'id'         => '_thing_' . 'links',
			'type'       => 'group',
			'repeatable' => true,
			'options'    => [
				'group_title'   => 'Link #{#}',
				'add_button'    => 'Add Another Link',
				'remove_button' => 'Remove Link',
				'sortable'      => true,
				'closed'        => true, // true to have the groups closed by default
			],
			'attributes' =>[
				'data-tooltip' => 'ListLinksGroupHelp',
			],
		]
	);

	$post_box->add_group_field( $group_field_id, array(
		'name' => esc_html__( 'Entry Title 1', 'cmb2' ),
		'id'   => 'title_1',
		'type' => 'text',
	) );

	$post_box->add_group_field( $group_field_id, array(
		'name' => esc_html__( 'Entry Title 2', 'cmb2' ),
		'id'   => 'title_2',
		'type' => 'text',
	) );

	$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'        => 3,
    'capability'      => 'manage_options', // Cap required to view options-page.
	];
	$cmb2_options_submenu2_info = new_cmb2_box($args);

	$args = [
    'id'              => 'cmb2_id_box_theme_options_3',
    'object_types'    => ['options-page'],
    'option_key'      => 'cmb_main_options3',
    'menu_title'      => __('Keepers3', 'keepers3'),
    'title'						=> __('Keepers3', 'keepers3'),
    'parent_slug'     => 'cmb_main_options',
    'position'        => 1,
    'capability'      => 'manage_options', // Cap required to view options-page.
	];
	$cmb2_options_submenu3_info = new_cmb2_box($args);



}
add_action( 'cmb2_admin_init', 'prueba_cmb2_init' );

keeper

@jtsternberg
Copy link
Member

you should be able to do this with the priority argument:

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

$post_box = new_cmb2_box( [
	'id'           => 'cmb2_id_box_theme_options_1',
	'object_types' => ['options-page'],
	'option_key'   => 'example_cb',
	'menu_title'   => __('Keepers1', 'keepers1'),
	'title'        => __('Keepers1', 'keepers1'),
	'parent_slug'  => 'cmb_main_options',
	'priority'     => 11,
	'capability'   => 'manage_options', // Cap required to view options-page.
] );
// ...

$cmb2_options_submenu2_info = new_cmb2_box([
	'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',
	'priority'     => 12,
	'capability'   => 'manage_options', // Cap required to view options-page.
]);
// ...

$cmb2_options_submenu3_info = new_cmb2_box([
	'id'           => 'cmb2_id_box_theme_options_3',
	'object_types' => ['options-page'],
	'option_key'   => 'cmb_main_options3',
	'menu_title'   => __('Keepers3', 'keepers3'),
	'title'        => __('Keepers3', 'keepers3'),
	'parent_slug'  => 'cmb_main_options',
	'priority'     => 10,
	'capability'   => 'manage_options', // Cap required to view options-page.
]);
// ...

@alfonmaco
Copy link
Author

your sample dont work, maybe you must use 'admin_menu_hook_priority' instead 'property', or change in includes/CMB2_Options_Hookup.php line 72 'admin_menu_hook_priority' for 'property'.

@jtsternberg
Copy link
Member

image

lipemat added a commit to lipemat/CMB2 that referenced this pull request 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add_submenu_page position / order not working

2 participants