Highlighting a Menu Item by Post Name

Attention to the use of get_page_by_title parameters.

And also to the use of the Heredoc PHP syntax.

$the_post_title="The Portfolio";

add_action( 'admin_menu', 'wpse_59050_add_menu' );
add_action( 'admin_head-post.php', 'wpse_59050_highlight_menu_item' );

function wpse_59050_add_menu() 
{
    global $the_post_title;
    $our_page = get_page_by_title( $the_post_title );

    $settings_page = add_menu_page( 'Edit '.$our_page->post_title, 'Edit '.$our_page->post_title, 'add_users', '/post.php?post=".$our_page->ID."&action=edit', '', '', 2);
}

function wpse_59050_highlight_menu_item()
{
    global $the_post_title, $post;

    if( !is_object( $post ) )
        return;

    $our_page = get_page_by_title( $the_post_title );

    if( $our_page->ID != $post->ID )
        return;

    echo <<<HTML
        <script type="text/javascript">
            jQuery(document).ready( function($) {
                $('#toplevel_page_post-post-{$our_page->ID}-action-edit').removeClass('wp-not-current-submenu').addClass('current');
                $('#toplevel_page_post-post-{$our_page->ID}-action-edit').find('a:last').addClass('current');
            });     
        </script>
HTML;
}

Result:
enter image description here

Leave a Comment