Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active January 4, 2022 15:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tommcfarlin/a0a7f83abac465a8fa0037fab4794135 to your computer and use it in GitHub Desktop.
Save tommcfarlin/a0a7f83abac465a8fa0037fab4794135 to your computer and use it in GitHub Desktop.
[WordPress] Highlight an Active Submenu in the Back-End
<?php
add_filter( 'parent_file', 'acme_highlight_draft_submenu' );
/**
* Highlights the 'Draft' submenu item using WordPress native styles.
*
* @param string $parent_file The filename of the parent menu.
*
* @return string $parent_file The filename of the parent menu.
*/
function acme_highlight_draft_submenu( $parent_file ) {
global $submenu_file;
// Only update the submenu file value if we're on the Draft page.
if ( 'draft' === get_query_var( 'post_status' ) ) {
$args = array( 'post_status' => 'draft', );
// Use the full URL so get the admin URL and concatenate it with the parent file.
$admin_url = trailingslashit( get_admin_url() );
$url = sprintf( '%s%s', $admin_url, $parent_file );
$submenu_file = add_query_arg( $args, $url );
}
return $parent_file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment