How can I move admin menu top-level items to submenu - wordpress

I'm using the code at https://markwilkinson.me/2014/11/altering-wordpress-admin-menus/ to change plugin-created top-level admin-menu items to submenu items of a 'misc' top-level item.
It works fine on those items which don't have their own submenu, but on items which have their own submenu the submenu disappears.
I don't know if the code can be appropriately modified, or if it can how to do it.

Wordpress does not allow you to add a 3rd level of menus in WP admin.
See the answer to How to Add a Third Level Sub Menu to the Wordpress Admin Menu from wordpress.stackexchange.com
The answer from Karthikeyani explains why it currently isn't possible:
The definition of add_submenu_page requires the parent slug name. For example:
add_menu_page ( 'Test Menu', 'Test Menu', 'read', 'testmainmenu', '', '' );
add_submenu_page ( 'testmainmenu', 'Test Menu', 'Child1', 'read', 'child1', '');
The first parameter of the add_submenu_page is parent slug name $parent_slug. You may think you could use child1 ($menu_slug) as parent slug name to create the third level, but this does not work.
The parameters definition and source section in the Wordpress developer documentation for add_submenu_page clearly states that you can only use the name of the main menu of the plugin or the file name of the WordPress plugin in parent slug name.
add_submenu_page Ref: https://developer.wordpress.org/reference/functions/add_submenu_page/

Related

Difference of menu and theme_location

I'm trying to create a menu by wp_nav_menu(), there are two argument seems the same although the name are different: menu and theme_location. Assume I already register a menu and the name/id is nice, now I want to display the menu on a page by wp_nav_menu(), this is what I do:
wp_nav_menu( array(
'menu' => 'nice',
'theme_location' => 'nice'
) );
If I don't specify the theme_location, it will still work? Because I already tell wordpress the menu I wanna display, and vice versa, if I specify the theme location, why do I still need to specify menu which I wanna use?
menu and theme_location has some explanations, menu is for specific menu id, slug, name or object where you will show that specific menu by the menu id, slug, name or object, on the other hand theme_location is where you want to show your menu. It is not necessary to set the menu and theme_location at a time, but if you set two options at a time it will priority the menu first then theme_location, in case you have no menu with that name/slug then it will check the theme_location, in case you set a wrong theme_location which is actually not registered then it will call the fallback_cb. Note that menu name comes from the admin panel https://prnt.sc/j-m6B-jY252k where theme_location comes from the functions.php file where you registered the menu https://prnt.sc/BglTqU_xows2 . This theme_location will show in the menu area in the admin panel https://prnt.sc/KY7cYottkJWe . So, in conclusion, you can use any one of it for showing the menu but it is totally up to you which way you will choose, but for developers it is wise to set theme_location instead of menu.
I have tried to explain everything from my end but if you have more questions you can read the official details of menu here

Wordpress plugin menu issue

I am building a wordpress plugin with the boilerplate framework (https://wppb.me/). That all works fine. I have an annoying issues which I cannot solve.
the boilerplate makes per object one php file. In this php file all funtionality for the object is arranged. Pages, lists, validations etc.
Example in the file for contact there is a method contactlog_menu:
Object: contact
public function contactlog_menu()
{
add_submenu_page('plugin-options', __('plugin', 'plugin'), __('am_contactlogs', 'plugin'), 'manage_options', 'contactlogs', array($this, 'plugin_contactlogs_page_handler'));
add_submenu_page('contactlogs', __('New contactlog', 'plugin'), __('Add new b', 'plugin'), 'activate_plugins', 'contactlogs_form', array($this, 'plugin_contactlogs_form_page_handler'));
}
Now it shows a menu option in the left menu of wordpress. I would like to remove this menu option and create a hyperlink on the dashboardpage to this page. If i remove the lines wordpress is telling me i have no permission to view the page. what am i missing here
So i want to remove the menu item but want to page to be accessible as a link on another page. Otherwise my menu will become very large :-).
I hope it is understandable, otherwise please ask..
If you use null for the parent_slug parameter the page won't show up in the menu.
add_submenu_page(
null,
'My Custom Submenu Page',
'My Custom Submenu Page',
'manage_options',
'my-custom-submenu-page',
'callback_function' );
The page will be accessible via any parent slug, as long as it exists:
https://yoursite.com/wp-admin/options-general.php?page=my-custom-submenu-page
https://yoursite.com/wp-admin/tools.php?page=my-custom-submenu-page
https://yoursite.com/wp-admin/index.php?page=my-custom-submenu-page
...
Or no parent slug at all (same effect as if you'd use index.php):
https://yoursite.com/wp-admin/?page=my-custom-submenu-page
But it's probably best to use admin.php for your hyperlinks (the menu item that belongs to the slug you use will be active - if you use admin.php no menu item will be active):
https://yoursite.com/wp-admin/admin.php?page=my-custom-submenu-page

Moving WordPress default menus in admin

I've been digging around the codex trying to find a way to move the default menus around in the admin area. specifically i want to take the customizer from the appearance menu and move it to my custom menu. same with other menus. Is there a list somewhere of for them or a file to edit with the orders or way to move them. I don't want to edit any core files though as obviously will break on any update.
I already have my own menu: image of own menu code is below for mine.
function dcmaintheme_menu() {
add_menu_page( 'dcmaintheme_adminpage', 'Dragon Cove Theme', 'manage_options', 'dcmaintheme_adminpage', 'dcmaintheme_page', content_url( 'themes/dragon-cove-base/img/icon.png' ), 10 );
add_submenu_page( 'dcmaintheme_adminpage', 'Admin Options', 'Admin Options', 'manage_options', 'submenu_adminoptions_page', 'submenu_adminoptions_page' );
what id like to get is another sub-menu that links to say the appearance->customiser and have it in my custom menu.
You can use this plugin, it will allow you to customize the Admin Toolbar and the Admin Side menu. You can choose what content on the menu to display to a specific user role.

Wordpress - Different sidebar for each page

Client needs an website to migrate into WordPress.
In this website each page has an sidebar with different content inside it
In some page accordion comes under sidebar, in some just text and images are visible
How to implement this in WordPress?
If template has to be created, there are many page it cant be done so
Even for every page different sidebar widget is not also possible
Guide me a way to implement this
A different sidebar (widget) are can be added to each page in two steps:
Add the sidebar to the theme template using the page slug as part of the sidebar name. This ensures that the sidebar has a unique name for that page.
Register the sidebars for each page in functions.php for your theme
Add the sidebar to the theme template
In your theme template, add the following code where you want the widget area to be:
<?php
global $post;
dynamic_sidebar( 'widget_area_for_page_'.$post->post_name );
?>
Register the sidebars
In functions.php for your theme add the following block of code to register the sidebars for each page in your site. Note that it includes draft pages etc so that you can edit the widgets while still in draft mode.
function myTheme_registerWidgetAreas() {
// Grab all pages except trashed
$pages = new WP_Query(Array(
'post_type' => 'page',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
'posts_per_page'=>-1
));
// Step through each page
while ( $pages->have_posts() ) {
$pages->the_post();
// Ignore pages with no slug
if ($pages->post->post_name == '') continue;
// Register the sidebar for the page. Note that the id has
// to match the name given in the theme template
register_sidebar( array(
'name' => $pages->post->post_name,
'id' => 'widget_area_for_page_'.$pages->post->post_name,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
}
add_action( 'widgets_init', 'myTheme_registerWidgetAreas' );
Hope it helps!
There's a great plugin for this:
https://wordpress.org/plugins/custom-sidebars/
Sometimes it is necessary to show different elements on the sidebars
for some posts or pages. The themes nowadays give you some areas to
put the widgets, but those areas are common for all the posts that are
using the same template. NOTE: You need to use a theme that accepts
widgets to make this plugin work.
I think this is what you're looking for.
This can be done with the https://wordpress.org/plugins/jetpack/ plugin. Once activated you can choose what widgets display on what pages:
The Widget Visibility module enables you to configure widgets to appear only on certain pages (or be hidden on certain pages) by using the Visibility panel.
Visibility is controlled by five aspects: page type, category, tag, date, and author. For example, if you wanted the Archives widget to only appear on category archives and error pages, choose “Show” from the first dropdown and then add two rules: “Page is 404 Error Page” and “Category is All Category Pages.”
Just tried custom-sidebars with the latest version of wp. Had a hard time deleting it. Database is still messy. Plugin did not work. Maybe the 'pro' version is useful, but the 'non-pro' version would not add a sidebar to the page. It was clunky and did not allow per-page sidebars. It says its not been tested with the latest wp, maybe that's the problem.
You can create custom sidebar for for each post and pages with the help of plugin. There are a few plugins which enables you to create your custom sidebar. Here in this tutorial we are using Easy Custom Sidebars. Download and install this plugin in your website and activate it.
After successfully activating the plugin go to Appearance > Sidebar Replacement
From this page give a name to your custom sidebar and click on the Create Sidebar button. After that go to the Sidebar Properties option. Here you need to select the properties where you want to replace your created custom sidebar instead of theme sidebar. You can view your available option in the drop-down menu. Select it and provide a description on the Sidebar Description field.
Now in the last step select the post or pages to add this custom sidebar. You can see all the option in the left column. You can select specific post, page, category , tag, author to add this sidebar. Just select it and click Add to sidebar option.
For better understanding you can follow this tutorial

main menu on wordpress theme is not carried over to new copy-pasted theme

Sorry, new to Wordpress here.
I copy-pasted an existing theme. Most of it looks fine. However, it seems the main menu style and menu items were not carried over to the new theme. The new theme is unstyled because it has no id menu-top, which the original one has. Also, the new theme has way too many menu items, while the original one only has 4.
What do I change so that my new theme's main menu gets that id, and will contain the same menu items?
I've check the Admin > Appearance > Menu and it correctly has 4 items, but it's not reflected in my new theme.
Also, in header.php, I tried adding 'menu_id' => 'menu-top' to:
wp_nav_menu( array( 'container'=>'none', 'theme_location' => 'primary' ) );
but that didn't change anything.
Help please?
I'm 99% sure, that the problem is that you don't have a menu selected in the "primary" theme location, so WordPress displays all of your pages.
To fix this, go to Appearance > Menus and see if you still have your old menu. If you do, just select this menu in the "Primary" drop-down on the left and click Save.
If you don't have the menu anymore, rebuild the menu and select it for the theme location. After that things should be fine.

Resources