Add a “Snippets” Custom Menu Item to the WordPress Admin Top Bar

Snipsnip.pro 2023 10 25 13.04.06

This code adds a menu item called “Snippets” to the WordPress Admin Bar, allowing users to quickly access Code Snippets Pro ❤️.

What it does…

This code snippet adds a new menu item called “Snippets” to the WordPress Admin Bar, which is the top menu bar visible when logged in as an admin user. The menu item provides a convenient way for users to access a dedicated page for managing code snippets. By clicking on the “Snippets” menu item, admin users are redirected to the “snippets” page in the WordPress admin area, where they can create, edit, and delete code snippets.

Why it does it…

This code enhances the WordPress admin experience by providing a dedicated menu item for managing code snippets. Code snippets are small pieces of reusable code that can be used to extend or modify WordPress functionality. By having a dedicated menu item, admin users can easily access and manage their code snippets without needing to navigate through various admin screens.

How it does it…

– The code checks if the Custom_Snippets_Menu class does not already exist to prevent redeclaration.
– If the class does not exist, it defines the Custom_Snippets_Menu class.
– The class has a constructor method that hooks into the admin_bar_menu action with a priority of 100.
– When the admin_bar_menu action is triggered, the add_toolbar_items method is executed.
– In the add_toolbar_items method, a new menu item with the ID snippets-page is added to the admin bar.
– The menu item has a title of “Snippets” and a URL that points to the admin.php?page=snippets page in the WordPress admin area.
– The menu item also includes a meta array that specifies the title attribute as “Snippets”.
– Finally, the Custom_Snippets_Menu class is instantiated, creating an instance of the class and adding the custom snippets menu to the WordPress Admin Bar.

Use the JSON file for easily importing into your CodeSnippetsPro plugin. Use the PHP file or just copy/paste the code sample into your functions.php if you’re not using CodeSnippetsPro.

Related Snips

Leave a Reply

Your email address will not be published. Required fields are marked *