Ordina le voci del menu amministratore
3 risposta
- voti
-
- 2013-01-08
Puòesserefatto ordinandoil
$submenu
globale.L'ordinamento applicato ripristinailnumero di chiave del sotto-array
$submenu['options-general.php']
,che è:array 'options-general.php' => array 10 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' 15 => array 0 => string 'Writing' 1 => string 'manage_options' 2 => string 'options-writing.php' // etc
e diventa:
array 'options-general.php' => array 0 => array 0 => string 'Discussion' 1 => string 'manage_options' 2 => string 'options-discussion.php' 1 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' // etc
Qui stiamo considerando la lunghezza deglielementipredefinitipari a sei. Prima di WordPress 3.5,c'erano 7elementi,la Privacy non èpiù disponibileed èincorporatanella scheda Lettura .
Testin locale,questononproduce risultatiimprevistie funzionabene. Forse se unplugin dipendesse da
$submenu['options-general.php'][15]
perposizionarsi o qualcos'altro,potrebbero verificarsi deibug.L'ordinamento viene applicato separatamenteperglielementipredefinitie peril resto diessi. Basta disabilitare
usort
delprimobloccoe hai l'output desiderato.add_action( 'admin_menu', 'sort_settings_menu_wpse_2331', 999 ); function sort_settings_menu_wpse_2331() { global $submenu; // Sort default items $default = array_slice( $submenu['options-general.php'], 0, 6, true ); usort( $default, 'sort_arra_asc_so_1597736' ); // Sort rest of items $length = count( $submenu['options-general.php'] ); $extra = array_slice( $submenu['options-general.php'], 6, $length, true ); usort( $extra, 'sort_arra_asc_so_1597736' ); // Apply $submenu['options-general.php'] = array_merge( $default, $extra ); } //http://stackoverflow.com/a/1597788/1287812 function sort_arra_asc_so_1597736( $item1, $item2 ) { if ($item1[0] == $item2[0]) return 0; return ( $item1[0] > $item2[0] ) ? 1 : -1; }
It can be done sorting the global
$submenu
.The sorting that's applied resets the key number of the sub-array
$submenu['options-general.php']
, which is:array 'options-general.php' => array 10 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' 15 => array 0 => string 'Writing' 1 => string 'manage_options' 2 => string 'options-writing.php' // etc
and becomes:
array 'options-general.php' => array 0 => array 0 => string 'Discussion' 1 => string 'manage_options' 2 => string 'options-discussion.php' 1 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' // etc
Here, we are considering the length of the default items equal to six. Prior to WordPress 3.5, there were 7 items, Privacy is now gone and embedded with the Reading tab.
Testing locally, this doesn't produce any unexpected result and works ok. Maybe if a plugin depended on
$submenu['options-general.php'][15]
to position itself or something else, bugs could happen.Sort is being applied separately for the default items and for the rest of them. Just disable the
usort
of the first block and you have your desired output.add_action( 'admin_menu', 'sort_settings_menu_wpse_2331', 999 ); function sort_settings_menu_wpse_2331() { global $submenu; // Sort default items $default = array_slice( $submenu['options-general.php'], 0, 6, true ); usort( $default, 'sort_arra_asc_so_1597736' ); // Sort rest of items $length = count( $submenu['options-general.php'] ); $extra = array_slice( $submenu['options-general.php'], 6, $length, true ); usort( $extra, 'sort_arra_asc_so_1597736' ); // Apply $submenu['options-general.php'] = array_merge( $default, $extra ); } //http://stackoverflow.com/a/1597788/1287812 function sort_arra_asc_so_1597736( $item1, $item2 ) { if ($item1[0] == $item2[0]) return 0; return ( $item1[0] > $item2[0] ) ? 1 : -1; }
-
- 2010-09-29
Stavo cercando la stessa cosae hopensato che avrei copiatoil codicefornito da Mike sottoil quale afferma che è ancoramoltoin alphapoiché è stato solo lui a usarlo.
L'hotestatoe sembrafunzionarebene perme. Sono sicuro che ci sono casi d'uso chenongestiscono ancora.
Forse Mike commenterà quie fornirà ulteriore aiuto su come utilizzare la sua classeper raggiungerei tuoi obiettivi.
Puoi scaricareilfile dainserirenella directory deltuotema qui: https://gist.github.com/792b7aa5b695d1092520
Quanto seguemostra come sipotrebbero chiamare lefunzioninelfilefunctions.php deltema:
<?php require_once('wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swap location of Posts Section with Pages Section rename_admin_menu_section('Media','Photos & Video'); // Rename Media Section to "Photos & Video" delete_admin_menu_section('Links'); // Get rid of Links Section $movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags'); // Save off the Movie Tags Menu update_admin_menu_section('Movies',array( // Rename two Movie Menu Items and Delete the Movie Tags Item array('rename-item','item'=>'Movies','new_title'=>'List Movies'), array('rename-item','item'=>'Add New','new_title'=>'Add Movie'), array('delete-item','item'=>'Movie Tags'), )); copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors renamed_admin_menu_item('Movies','Add New','Add Actor'); // Rename copied Actor 'Add New' to 'Add Actor add_admin_menu_item('Movies',array( // (Another way to get a 'Add Actor' Link to a section.) 'title' => 'Alt Add Actor ', 'slug' => 'post-new.php?post_type=actor', ), array(// Add Back the Movie Tags at the end. 'where'=>'end' )); add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end. 'where'=>'end' )); delete_admin_menu_section('Actors'); // Finally just get rid of the actors section }
Inoltre,questefunzioni sono anchepresein considerazione (comebase)per l'inclusionein WordPress 3.1,quindi se siamofortunati questepotrebberopersino diventare standard!
I was looking for the same thing and figured I would copy code provide by Mike below which he claims is still very much in alpha since its only been him that has been using it.
I have tested it and it seems to work well for me. I'm sure there are use-cases they do not yet handle.
Maybe Mike will comment in here and provide additional help on how you can use his class to achieve your goals.
You can download the file to drop in your theme's directory here: https://gist.github.com/792b7aa5b695d1092520
What follows below shows how you might call the functions in your theme's functions.php file:
<?php require_once('wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swap location of Posts Section with Pages Section rename_admin_menu_section('Media','Photos & Video'); // Rename Media Section to "Photos & Video" delete_admin_menu_section('Links'); // Get rid of Links Section $movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags'); // Save off the Movie Tags Menu update_admin_menu_section('Movies',array( // Rename two Movie Menu Items and Delete the Movie Tags Item array('rename-item','item'=>'Movies','new_title'=>'List Movies'), array('rename-item','item'=>'Add New','new_title'=>'Add Movie'), array('delete-item','item'=>'Movie Tags'), )); copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors renamed_admin_menu_item('Movies','Add New','Add Actor'); // Rename copied Actor 'Add New' to 'Add Actor add_admin_menu_item('Movies',array( // (Another way to get a 'Add Actor' Link to a section.) 'title' => 'Alt Add Actor ', 'slug' => 'post-new.php?post_type=actor', ), array(// Add Back the Movie Tags at the end. 'where'=>'end' )); add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end. 'where'=>'end' )); delete_admin_menu_section('Actors'); // Finally just get rid of the actors section }
What's more, these functions are even under consideration (as a base) for inclusion in WordPress 3.1 so if we're lucky these might even become standard!
-
Sembra unbelpo 'di codice,manon vedo dove ordina automaticamente le voci secondarie.It sounds like a great bit of code, but I don't see where it automatically sorts sub-entries.
- 0
- 2010-09-30
- dgw
-
- 2017-09-07
Hopensato chepotesseessere utile come altra opzione. Ecco un semplice utilizzonel sottomenu $globale. Uso l'hook admin_init,perché admin_menupotrebbe ancora avere altriplugin che aggiungonoelementi di sottomenu (nelmio caso lo hafatto,quindieratroppoprestopereseguire l'ordinamento). Nota anche che lafunzione anonima (lambda)nell'usort richiede PHP 5.3+.
<?php add_action('admin_init','my_admin_init'); function my_admin_init() { global $submenu; $submenu_key = 'my_key'; // See note below on $submenu_key if ( array_key_exists( $submenu_key, $submenu ) ) { // alphabetize submenu items usort( $submenu[ $submenu_key ], function ( $a, $b ) { // format of a submenu item is [ 'My Item', 'read', 'manage-my-items', 'My Item' ] return strcasecmp( $a[0], $b[0] ); } ); } }
Tienipresente che
$submenu_key
è$parent_slug
in add_submenu_page ,cheequivale a$menu_slug
in add_menu_page .Thought this could be useful as another option. Here is a simple usort on the global $submenu. I use admin_init hook, because admin_menu may still have other plugins adding submenu items (in my case it did, so that was too early to do sorting). Also note the anonymous (lambda) function in the usort requires PHP 5.3+.
<?php add_action('admin_init','my_admin_init'); function my_admin_init() { global $submenu; $submenu_key = 'my_key'; // See note below on $submenu_key if ( array_key_exists( $submenu_key, $submenu ) ) { // alphabetize submenu items usort( $submenu[ $submenu_key ], function ( $a, $b ) { // format of a submenu item is [ 'My Item', 'read', 'manage-my-items', 'My Item' ] return strcasecmp( $a[0], $b[0] ); } ); } }
Note that
$submenu_key
is$parent_slug
in add_submenu_page, which is the same as$menu_slug
in add_menu_page.-
Belloe compatto.Cosa rappresenta "$ submenu_key"?È questo che WordPress chiama "$parent slug"nella documentazione [add_submenu_page] (https://developer.wordpress.org/reference/functions/add_submenu_page/)?Nice and compact. What is `$submenu_key` representing? Is that what WordPress calls the `$parent slug` in the [add_submenu_page](https://developer.wordpress.org/reference/functions/add_submenu_page/) docs?
- 1
- 2017-09-08
- random_user_name
-
@cale_b Ottima domanda!Hai ragione,è "$parent_slug"in add_submenu_page docs,che è lo stesso di "$menu_slug"in add_menu_page.@cale_b Great question! You are correct, it is `$parent_slug` in add_submenu_page docs, which is the same as `$menu_slug` in add_menu_page.
- 0
- 2017-09-08
- ryanm
In unanota correlata alla " Modifica dell'ordine delle sezioni delmenu di amministrazione? ",sto cercando unmodoper ordinare alfabeticamente le vociin ogni sottosezione dell'area di amministrazione di WordPress.
Attualmente,ogni volta che viene aggiunto unnuovoplugin,la sua voce appariràin unaposizione apparentemente casualein Impostazioni/Strumenti/Plugin,ed è spesso difficileindividuare lanuova voce dimenu. (Hogiàmoltiplugin,quindii mieimenu sonopiuttostopieni.)
Dato che aggiungoe rimuovoplug-in abbastanza regolarmente,preferireinon dover continuamente accedere allapagina delleimpostazioniper unplug-inper l'ordinamento deimenue regolare l'ordine.
Ci scusiamoper la lunga domanda; Voglio solo chiarire cosa sto cercando.
Esempio
Invece di:
Posso avereilmenuin ordine alfabetico (o con un altrometodo logico),adesempio:
Ancorameglio sarebbe unmetodo di ordinamento chemantenga le vociprincipali dove sonoperimpostazionepredefinitae ordina sologlielementi aggiunti daiplugin: