I’m developing a child-theme using WordPress 3.4.2 and the development version of the Options Framework by David Price. This is my first theme and I’m relatively new to this, so I’ve had a look into the WordPress Codex and checked out registering items into the API.
Without tampering with any external files outside of my theme, I was wondering if there was a way to re-arrange where the Theme Options page is located within the hierarchy of the Appearance menu – so when my theme is activated, the position is not like the first image but like the second.
I know you can create either a menu (such as the Appearance tab, Plugins, Users etc.) or a sub-menu (Themes, Widgets, Menus etc.), but how would I go about setting a sub-menu say, second from the top?
From what I gather, somewhere there is an order being called and any other additional pages within the functions.php
file are placed after those?
In my functions.php file:
// Add our "Theme Options" page to the WordPress API admin menu.
if ( !function_exists( 'optionsframework_init' ) ) {
define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/inc/' );
require_once dirname( __FILE__ ) . '/inc/options-framework.php';
}
Thanks.
Here’s an example;
First to figure out the order of the sub menu items based upon its array key you can do a
var_dump
on the $submenu global variable which will output the following;(I’m using the Posts menu and sub menu as an example)
We can see that my sub menu item gets added into the array with a key of 17 after the default items.
If for example I want to add my sub menu item, directly after the All Posts sub menu item I need to do so by setting my array key to either 6, 7, 8 or 9 (anything after 5 and before 10 respectively.
This is how you do it…
Result,
…give it a try and let us know how you go!
Update 1:
Add this to your functions.php file;
My update includes a slightly easier way to handle the setting of your menu position, you need only stipulate the name of your submenu page and the position you want within the menu.
However if you select a submenu pageTo circumvent that, Kaiser’s example provides some basic checking for that.$location
equal to that of an existing key, it will override that key with yours, thus the menu item will disappear with your menu item in its place. Increment or decrement the number to correctly order your menu if that is the case. Similar, if someone installs a plugin that effects that same menu area, and for which has the same$location
as your submenu item then the same problem will occur.Update 2:
I’ve added an additional block of code that checks all existing keys in the array against our desired
$location
and if a match is found we will increment our$location
value by1
in order to avoid menu items overriding each other. This is the code responsible for that,Update 3: (script revised to allow sorting of multiple sub menu items)
In the example above you can target multiple sub menus and multiple items per sub menu by setting the parameters accordingly within the
$target_menu
variable which holds a multi-dimensional array of values.This revision will prevent sub menu items over-writing each other if they have the same key (position), as it will cycle through until it finds an available key (position) that does not exist.
The admin menu (and its problems)
As the admin menu seriously lacks any hooks and a public API (that allow moving the items around), you have to use some workarounds. The following answer shows you what is awaiting you in the future and how you can work around as long as we have the current state of core.
First I have to note, that scribu is working on an admin menu patch that should make the handling much easier. The current structure is pretty messed up and I have written an article about it that will soon be outdated. Expect WP 3.6 to change things completely.
Then there’s also the point, that you shouldn’t use Options pages for themes anymore. There’s – nowadays – the »Theme Customizer« for that.
The plugin
I wrote a plugin that tests this with the default “Theme Options” page for the TwentyEleven/Ten options page. As you can see, there’s no real API that allows any position. So we have to intercept the global.
In short: Just follow the comments and take a look at the admin notices, that I added to give you some debug output.
Good luck and have fun.
Custom filters
There is be another possibility to achieve this. Don’t ask me why I haven’t thought earlier about it. Anyway, there’s a filter dedicated to a custom menu order. Simply set it to
true
to allow a custom order. Then you got a second hook to order the main menu items. In there we just intercept theglobal $submenu
and switch around our sub menu items.This example moves the Menus item above the Widgets item to demonstrate its functionality. You can adjust it to what you like.