Snippet

Add Custom Icons to WordPress Admin Menu Easily

How to add custom icons to wordpress admin menuWordpress admin menu custom icon tutorialChange wordpress admin menu iconsAdd svg icon to wordpress admin menuCustomize wordpress admin menu iconsWordpress admin menu icon pluginCustom icon for wordpress admin dashboardWordpress admin menu icon changeAdd custom image to wordpress admin menuWordpress admin menu icon customization

Explanation

Want to jazz up your WordPress admin menu with custom icons? Here's how you can do it without breaking a sweat.

Change Existing Icons:

  • To change the icon for the Dashboard, use a special code called Dashicons. For example, '\f120' changes the Dashboard icon.
  • Similarly, you can change the Posts menu icon using '\f109'.

Add Custom SVG Icons:

  • If you have a specific SVG icon, you can set it for a menu item by specifying the path to your SVG file. This will replace the default icon with your custom image.
  • Ensure your SVG file is accessible and the path is correct.

Create a New Menu with a Custom Icon:

  • You can add a new menu item with a default Dashicon. For instance, 'dashicons-admin-generic' is used here.
  • To replace this with your SVG, use the same method as above by specifying the SVG path.

These tweaks are added to the admin area using a bit of CSS magic. Just make sure to place the code in your theme's functions file, and you're good to go!

Code

1// Function to add custom icons to WordPress admin menu 2function wp_dudecom_custom_admin_menu_icons() { 3 ?> 4 <style> 5 /* Change the icon for the Dashboard menu item */ 6 #adminmenu #menu-dashboard div.wp-menu-image:before { 7 content: '\f120'; /* Dashicons code for a custom icon */ 8 } 9 10 /* Add a custom SVG icon to a specific menu item */ 11 #adminmenu #toplevel_page_custom_menu div.wp-menu-image { 12 background-image: url('path-to-your-icon.svg'); 13 background-repeat: no-repeat; 14 background-position: center; 15 background-size: contain; 16 } 17 18 /* Example: Change the icon for the Posts menu item */ 19 #adminmenu #menu-posts div.wp-menu-image:before { 20 content: '\f109'; /* Dashicons code for a custom icon */ 21 } 22 </style> 23 <?php 24} 25add_action('admin_head', 'wp_dudecom_custom_admin_menu_icons'); 26 27// Function to register a custom admin menu with a custom icon 28function wp_dudecom_register_custom_menu_page() { 29 add_menu_page( 30 __('Custom Menu Title', 'textdomain'), 31 __('Custom Menu', 'textdomain'), 32 'manage_options', 33 'custom_menu_slug', 34 'wp_dudecom_custom_menu_page', 35 'dashicons-admin-generic', // Default Dashicon 36 6 37 ); 38} 39add_action('admin_menu', 'wp_dudecom_register_custom_menu_page'); 40 41// Callback function for the custom menu page 42function wp_dudecom_custom_menu_page() { 43 echo '<h1>' . __('Welcome to the Custom Menu Page', 'textdomain') . '</h1>'; 44} 45 46// Function to enqueue custom SVG icon for the custom menu 47function wp_dudecom_enqueue_custom_admin_icons() { 48 ?> 49 <style> 50 #adminmenu #toplevel_page_custom_menu_slug div.wp-menu-image { 51 background-image: url('path-to-your-custom-icon.svg'); 52 background-repeat: no-repeat; 53 background-position: center; 54 background-size: contain; 55 } 56 </style> 57 <?php 58} 59add_action('admin_head', 'wp_dudecom_enqueue_custom_admin_icons');

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have access to your WordPress theme files or a custom plugin.
  • Have the path to your custom SVG icons ready if you plan to use them.

Implementation Steps:

  1. Open your WordPress theme's functions.php file or create a new custom plugin file.
  2. Copy and paste the provided code into the file.
  3. Replace 'path-to-your-icon.svg' and 'path-to-your-custom-icon.svg' with the actual paths to your SVG files.
  4. Save the changes to the file.
  5. Log in to your WordPress admin dashboard to see the changes in the menu icons.

Notes:

  • Ensure your SVG files are uploaded to your server and the paths are correct.
  • If you encounter any issues, double-check the code for any syntax errors.

Need help with implementation or want more advanced functionality? Visit wp-dude.com for expert WordPress assistance.