Skip to content

Instantly share code, notes, and snippets.

@RadGH
Created April 15, 2022 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadGH/87e2fa86d1863e0022dea4cb94016f49 to your computer and use it in GitHub Desktop.
Save RadGH/87e2fa86d1863e0022dea4cb94016f49 to your computer and use it in GitHub Desktop.
Change active parent menu and child menu items in WordPress Dashboard sidebar
<?php
// Below are some example fixes for a structure such as this:
//
// Post Type: lesson (Hidden from menu)
// Taxonomy: course (Hidden from menu)
// Taxonomy: cohort (Hidden from menu)
// And a Gravity Form with ID 3, which links to the Entries page
//
// Note that cm_is_taxonomy_page() and cm_is_homework_entries_page() check the current page using $pagenow, $typenow, and $_GET.
// They are not included
// Fix the Courses and Entries dashboard pages. They do not get highlighted properly.
function cm_student_dashboard_fix_course_menu($parent_file) {
global $plugin_page;
$post_type_slug = 'edit.php?post_type=lesson';
$tax_course_slug = 'edit-tags.php?taxonomy=course&post_type=lesson';
$tax_cohort_slug = 'edit-tags.php?taxonomy=cohort&post_type=lesson';
if ( cm_is_taxonomy_page('course') ) {
$parent_file = $post_type_slug;
$plugin_page = $tax_course_slug;
}
if ( cm_is_taxonomy_page('cohort') ) {
$parent_file = $post_type_slug;
$plugin_page = $tax_cohort_slug;
}
// taxonomy file ($plugin_page) also needed for this page for some reason
// @see https://stackoverflow.com/a/28856349
if ( cm_is_homework_entries_page() ) {
$parent_file = $post_type_slug;
$plugin_page = $tax_course_slug;
}
return $parent_file;
}
add_filter('parent_file', 'cm_student_dashboard_fix_course_menu', 30);
// Fix the submenu_file to the actual url that is used for our pages
function cm_student_dashboard_fix_course_submenu($submenu_file) {
if ( cm_is_taxonomy_page('course') ) {
$submenu_file = 'edit-tags.php?taxonomy=course&post_type=lesson';
}
if ( cm_is_taxonomy_page('cohort') ) {
$submenu_file = 'edit-tags.php?taxonomy=cohort&post_type=lesson';
}
if ( cm_is_homework_entries_page() ) {
$submenu_file = 'admin.php?page=gf_entries&id=3';
}
return $submenu_file;
}
add_filter( 'submenu_file', 'cm_student_dashboard_fix_course_submenu', 30);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment