wordpress php hardcode item to submenu - php

I'm trying to add an item to a sub menu via functions.php as the item will change based on the users logged in status and subscription status.
Here is the code I use to add an item to the menu:
add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
if (is_single() && $args->theme_location == 'primary') {
$items .= '<li>Show whatever</li>';
}
return $items;
}
However, does anyone know how I would add an item to a submenu?

You can use add_menu_subpage wordpress' function.
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page(
null //or 'options.php'
, 'My Custom Submenu Page'
, 'My Custom Submenu Page'
, 'manage_options'
, 'my-custom-submenu-page'
, 'my_custom_submenu_page_callback'
);
}
Reference: http://codex.wordpress.org/Function_Reference/add_submenu_page

Related

Change the "WooCommerce" Dashboard Menu option name into "Store"

Based on Change WooCommerce products menu title in WordPress admin dashboard answer by 7uc13r, I am trying to understand how to change WooCommerce into Store.
I've tried with the following, without success:
#1: Debug, show the menu array on dashboard
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
#2: change the name and submenu name (WooCommerce won't change, "All products" does)
function custom_change_admin_label() {
global $menu, $submenu;
$menu[70][0] = 'Store';
$submenu['edit.php?post_type=product'][5][0] = 'All Plugins';
}
add_action( 'admin_menu', 'custom_change_admin_label' );
This should suffice to change WooCommerce into Store
function custom_change_admin_label() {
global $menu, $submenu;
// Change WooCommerce to Store
$menu['55.5'][0] = 'Store';
}
add_action( 'admin_menu', 'custom_change_admin_label' );
In conjunction with your previous question you will get:
function custom_change_admin_label() {
global $menu, $submenu;
// Change 'WooCommerce' to 'Store'
$menu['55.5'][0] = 'Store';
// Change 'All Products' to 'All Plugins'
$submenu['edit.php?post_type=product'][5][0] = 'All Plugins';
// Contains the URI of the current page.
$current_url = $_SERVER['REQUEST_URI'];
// Make sure wc-admin / customers page will still work
if ( strpos( $current_url, 'customers' ) == false) {
// Remove 'Home' from WooCommerce menu
remove_submenu_page( 'woocommerce', 'wc-admin' );
}
}
add_action( 'admin_menu', 'custom_change_admin_label', 99, 0 );

Change WooCommerce products menu title in WordPress admin dashboard

We have tried to change the products page at the back end title menu but we couldn't using the snippet below in the screenshot:
We need to change both the menu title "Products" to " New Title " & Sumbmenu "All Products" to " All Submenu "
add_filter( 'gettext', 'custom_translate_woocommerce_strings', 999, 3 );
function custom_translate_woocommerce_strings( $translated, $text, $domain ) {
$translated = str_ireplace( 'Product', 'New Title', $translated );
return $translated;
}
The first part of the code is to debug, this will show you the menu in detail on the dashboard. (you can remove this afterwards)
The 2nd part in this example adds the changes
It is therefore a matter of adjusting based on the detail
// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only.
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
// Change label
function custom_change_admin_label() {
global $menu, $submenu;
$menu[26][0] = 'New Title';
$submenu['edit.php?post_type=product'][5][0] = 'All Submenu';
}
add_action( 'admin_menu', 'custom_change_admin_label' );

How to rename a menu tab under WooCommerce tab on WordPress admin dashboard

I need help in renaming a tab menu item under woocommerce tab on wordpress admin. We installed a plugin that appears as a submenu on woocommerce tab. Can anyone please help me on this?
I found this code below to rename a tab menu, but I dont know what is the tabmenu key of it. Or anyone here how to check tab menu key on my current tab menu items?
add_action( 'admin_menu', 'custom_change_admin_label', 99);
function custom_change_admin_label() {
global $menu;
//global $submenu;
$menu[5][0] = 'Articles';
}
Thank you
The first part of the code is to debug, this will show you the menu in detail on the dashboard. (you can remove this afterwards)
The 2nd part in this example changes the 'coupons' label to 'voucher'
It is therefore a matter of adjusting based on the detail
// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only.
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
// Change label, in this example changes the 'coupons' label to 'voucher'
function custom_change_admin_label() {
global $menu, $submenu;
$submenu['woocommerce'][2][0] = 'Voucher'; // rename 'coupons' label
}
add_action( 'admin_menu', 'custom_change_admin_label' );

Dynamically edit/add menu item - Wordpress

I want to change menu label of items accordingly if a user is logged in or not .
If user is not logged in i want to show Login and if user is admin I want to show Admin page and if normal user is logged in i want to show My Profile page .
I have tried this code.
add_filter( 'wp_nav_menu_items', 'dynamic_label_change', 10, 2 );
function dynamic_label_change( $items, $args )
{
if (!is_user_logged_in() && $args->theme_location == 'topbar_navigation')
{
$items = str_replace("Login", "Profile", $items);
}
return $items;
}
If you want to add new menu item then below code will help.
// Add Login / Logout menu item dynamically to primary navigation menu
function custom_menu_links( $items, $args ) {
if ($args->theme_location == 'primary'){
if (is_user_logged_in()) {
$items .= '<li>Logout</li>';
} else {
$items .= '<li>Login</li>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'custom_menu_links', 10, 2 );

Reorganization WordPress boxes in admin area

Actually, I have two questions here.
1: How to hide some boxes in WordPress area (for example, categories box or comments box) for subscribers?
2: How to sort categories in category box by id (looks like, by default, it sorted by name). For example, I would like to see
the next order: 1, 2, 3...
but not 10, 1, 11
For Question-2,
Please Try below code.
function sort_get_terms_args( $args, $taxonomies ) {
global $pagenow;
if( !is_admin() || ('post.php' != $pagenow && 'post-new.php' != $pagenow) )
return $args;
$args['orderby'] = 'term_id';
$args['order'] = 'ASC';
return $args;
}
add_filter( 'get_terms_args', 'sort_get_terms_args', 10, 2 );
Hope this will helpful for you !
For Question-1,
Add code as per below in your active theme's functions.php file
function my_remove_meta_boxes() {
if ( ! current_user_can( 'manage_options' ) ) {
/**
* arg-1 : id of the metabox, arg-2: page or posttype, arg-3: screen where the boxes displaying
*/
remove_meta_box( 'tagsdiv-stages', 'post', 'side' ); // as per my page https://www.screencast.com/t/VCnvf61M7ydS
remove_meta_box( 'commentsdiv', 'post', 'normal' );
}
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
You can refer : https://codex.wordpress.org/Function_Reference/remove_meta_box

Categories