WordPress admin submenu links incorrectly ordered - php

In the plugin I'm working on, I have an admin menu item, with several submenus depending on whether certain features of the plugin are enabled. Because of this, the submenus are declared in different classes.
The way I'm declaring the submenus:
add_action("admin_menu", array($this,"a_function"));
function a_function(){
add_submenu_page(
"a_parent_slug", //parent slug
__("a title","a-text-domain"), //page title
__("a title","a-text-domain"), //menu title
"manage_options", //capability
"a_slug", //menu slug
array($this, "a_callback_function"), //callback
40 //position
);
}
The positions are all declared as integers.
The order they're appearing in the actual menu, however, is this:
10
20
30
31
32
50
40
Using floats instead of integers made it worse, they appeared in an all but random order.
Am I missing something? Or is this a bug I should be raising on Trac?

Solution: the position in the function doesn't appear to do squat - changing the priority of the action did the trick. eg.
add_action("admin_menu", array($this,"a_function"),40);
function a_function(){
add_submenu_page(
"a_parent_slug", //parent slug
__("a title","a-text-domain"), //page title
__("a title","a-text-domain"), //menu title
"manage_options", //capability
"a_slug", //menu slug
array($this, "a_callback_function"), //callback
40 //position
);
}

Related

Manipulate WooCommerce category description output

I have around 4000 categories in WooCommerce and I don't have the time to go through every category and manually update the category descriptions. I am wondering if it would be possible to output a generic category description based on the H1 tag?
For example:
if the title is 'Ford Focus Car Spares'
The category description would autofill to something like:
'Buy Ford Focus Car Spares - We stock a huge range of Ford Car Spares'
I would need a way of programmatically removing the keywords from the h1 title and outputting them into the description.
Thankyou to anybody that can help.
You can hook the display of the title of your category page
add_action( 'woocommerce_archive_description', 'custom_category_description', 10, 2);
function custom_category_description($woocommerce_taxonomy_archive_description, $int) {
if ( is_product_category()) {
$title_cat = get_the_title($int);
echo 'Buy '.$title_cat.' - We stock a huge range of '.$title_cat;
}
}
I haven't tested this part of code but this is what I will explore

Issue when adding an admin menu to a WP plugin

I have the following function which creates the menus in the admin area for my plugin.
function register_menu_items() {
add_menu_page(null, 'Ebay Motors', 'manage_options', 'ebay-motors-admin', array($this, 'ebay_motors_admin'));
add_submenu_page( 'ebay-motors-admin', 'Upload XML', 'Upload XML', 'manage_options', 'upload-xml', array($this, 'upload_xml'));
add_submenu_page( 'ebay-motors-admin', 'Archive XML', 'Archive XML', 'manage_options', 'archive_xml', array($this, 'archive_xml'));
}
This works, however (expectedly) the main menu item also creates a sub-menu item. Is there a way to create the main menu item without intern creating a submenu item. I'd also like the Main Menu item to link straight to the upload_xml function.
You can set the slug of your sub menu to be the same of the main menu item. This way you can overwrite the first menu item.

Wordpress add_submenu_page function not working

I need to create a new menu and a sub menu item for the wordpress administrators. The menu is working fine but the sub menu items ( a duplicate sub menu for the main menu item is being created automatically.. I don't want that ). Both the sub menu items has their href attribute blank. I don't understand what the menu slug is. Please help..
// create custom plugin settings menu
add_action('admin_menu', 'retail_create_menu');
function retail_create_menu() { //create new top-level menu
add_menu_page('Retailers', 'Retailers', 'administrator', __FILE__, 'generate_retailer_list', 'http://localhost/apsm/wp-content/themes/wp-mediamag/functions/retail.ico');
add_submenu_page( __FILE__, 'Add Retailers', 'Add Retailers', 'administrator', 'add_ratilers.php', 'retailer_submenu_callback');
}
The file are inside a folder in my theme...
One simple hack to hide that duplicate menu:
add_submenu_page(
'__FILE__', // parent slug, same as main menu slug
'', // empty page title
'', // empty menu title
'administrator', // same capability as parent
'__FILE__', // same menu slug as parent slug
'generate_retailer_list', // same function as parent
)

each plugin i make i have a issue with the add menu and submenu pages

So what i mean by the above is. I've so far made many plugins but i keep getting this issue which i don't know how to describe and that is why i cant search it on google.
What happen is in many plugin a main menu is required and under it several sub-menus that leads to different pages related to the plugin. you can click on the main menu lets say "car" and that will take you to a add car page. and there will also be a sub-menu called add car.
like: posts - when you click on posts for example it takes you to all post, but there is also a sub-menu that is called all post.
What i am having problem with is the main menu is named one thing and when hover it the first sub-menu is also called the same.
My question is:
How can i make it so that either when creating a main menu it will not automatically add a sub menu or if it does. the name will be different then the main menu.
I hope the explanation is clear - i really tried.
Here is how i define my menus:
function my_plugin_menu() {
add_menu_page( 'add_deal', 'Add Deal', 'manage_options', 'add_deal', 'menuload_add_deal', '', '28');
add_submenu_page( 'add_deal', 'deals', 'Deals', 'manage_options', 'deals', 'menuload_deals');
add_submenu_page( 'add_deal', 'orders', 'Order', 'manage_options', 'orders', 'menuload_orders');
add_submenu_page( 'add_deal', 'gc_options', 'Options', 'manage_options', 'gc_options', 'menuload_gc_options');
}
add_action( 'admin_menu', 'my_plugin_menu' );
The above now display for me:
Add Deal - //opens add deal page
Deals
Orders
Options
I would like to change it to:
Shopping Cart - //opens add deal page
add deal - //opens add deal page
deals
orders
options
Ones again i hope that makes sense.. not trying to waste anyone's time but that's why its so hard for me to find info on googble about this issue. (i don't know how to describe the problem well).
I've found a solution incase anyone interested:
function my_plugin_menu() {
add_menu_page( 'add_product', 'Bass Nation', 'manage_options', 'bass-nation-real-shopping-cart', 'menu_load_add_product', '', '28');
add_submenu_page( 'bass-nation-real-shopping-cart', 'add_product', 'Add Product', 'manage_options', 'bass-nation-real-shopping-cart', 'menu_load_add_product');
add_submenu_page( 'bass-nation-real-shopping-cart', 'products', 'Products', 'manage_options', 'products', 'menu_load_products');
add_submenu_page( 'bass-nation-real-shopping-cart', 'orders', 'Orders', 'manage_options', 'orders', 'menu_load_orders');
add_submenu_page( 'bass-nation-real-shopping-cart', 'bnrsc_options', 'Options', 'manage_options', 'bnrsc_options', 'menu_load_options');
}
add_action( 'admin_menu', 'my_plugin_menu' );
You just name the first sub menu's slugen same as the main menu.
Hope this helps someone out there.

Make Menus in Wordpress use menu-item name instead of number for list item ID?

So after calling wp_nav_menu like:
<?php wp_nav_menu( array('menu' => 'Primary Menu' )); ?>
I end up with:
<li id="menu-item-1">
<li id="menu-item-2">
... ids like menu-item-1, menu-item-2, etc.
Is there some way to replace the numbers with the title of the page instead,so
menu-item-1 becomes menu-item-contact,
menu-item-2 becomes menu-item-store, etc?
You'll need to filter the ID using WordPress's nav_menu_item_id filter, which is applied in the nav-menu-template.php file. It takes three arguments, but the second one is what we need to actually get the title of the menu item, and turn it into something useful for the ID attribute. Here's a function to do that, which works for me in WordPress 3.4.1:
function custom_nav_menu_item_id($id, $item) {
$title = sanitize_title_with_dashes($item->title);
return 'menu-item-'.$title;
}
add_filter('nav_menu_item_id', 'custom_nav_menu_item_id', 10, 2);
This uses another WordPress function to sanitize the title into a string usable for the ID attribute (or any other HTML attribute).
Note that this function get's the menu item's title, which migth be different than the post or page title if you change it in the Menu panel from the admin.
Based on #Jeremy answer : I just add sanitize_title() to remove accents and special char.
function custom_nav_menu_item_id($id, $item) {
$title = sanitize_title_with_dashes(sanitize_title($item->title));
return 'menu-item-'.$title;
}
add_filter('nav_menu_item_id', 'custom_nav_menu_item_id', 10, 2);
/** for a better wp cleaning take a look # this gist and include it in your function.php */

Categories