• Branko

    (@lookiz)


    I’ve been trying for the life of me to figure out how to change the default “thumbtack” icon of the “Posts” menu on the backend to something else. Unfortunately, when searching Google for this, all I get is how to do it with Custom Post Types—not the default/existing “post” post-type. I know with a Custom Post Type you use ‘menu_icon’ in your args when registering the Custom Post Type, but I can’t figure out how to do it with ‘post’.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Lucas Karpiuk

    (@karpstrucking)

    You can use the admin_menu action hook to modify the $menu global. You can print_r( $menu ); to see the output. Here’s an example that changes the dashicon used:

    add_action( 'admin_menu', 'gowp_admin_menu' );
    function gowp_admin_menu() {
      global $menu;
      foreach ( $menu as $key => $val ) {
        if ( 'Posts' == $val[0] ) {
          $menu[$key][6] = 'dashicons-editor-paragraph';
        }
      }
    }

    You can also specify the URL of an image instead of a dash icon. I haven’t tried it, but you should also be able to specify an image as a data URI.

    Thread Starter Branko

    (@lookiz)

    hello karpstrucking thanks,

    I just need to define a specific url image, which is located in the images directory of my theme, you can give me an example?
    excuse but are a novice in the field.
    thanks

    Lucas Karpiuk

    (@karpstrucking)

    You can use the get_stylesheet_directory_uri() function to return the URL of the current theme, and then append the folder/filename:

    $menu[$key][6] = get_stylesheet_directory_uri() . '/images/icon.png';
    Thread Starter Branko

    (@lookiz)

    ok i tried to enter the code in the file function.php, refresh the page, but the icon is not changed. Wp version 4.2.2.

    Lucas Karpiuk

    (@karpstrucking)

    Can you copy and paste the exact code you’re using in your functions.php file? (put the code in between backticks to preserve formatting)

    Thread Starter Branko

    (@lookiz)

    here it is:

    add_action( 'admin_menu', 'gowp_admin_menu' );
    function gowp_admin_menu() {
      global $menu;
      foreach ( $menu as $key => $val ) {
        if ( 'Posts' == $val[0] ) {
          $menu[$key][6] = get_stylesheet_directory_uri() . '/images/flag_uk.png';
        }
      }
    }
    Lucas Karpiuk

    (@karpstrucking)

    Strange, I just copy/pasted exactly that into a test site and it worked (well, the image didn’t load obviously, as it doesn’t exist – but it tried).

    Are you using any plugins that modify the admin menu? Are you using a language other than en_US?

    Thread Starter Branko

    (@lookiz)

    How language use it_IT
    do not use any plugin for menus
    I tried again, but the picture does not change

    Lucas Karpiuk

    (@karpstrucking)

    Ah, if you’re using it_IT you’ll need to change “Posts” to “Articoli” in the code.

    Thread Starter Branko

    (@lookiz)

    or very well, now he works perfectly!
    thanks for your help and your time

    More universal solution is to use inline translation – __('PHRASE TO TRANSLATE') than icon will be substituted in any language:

    add_action( 'admin_menu', 'gowp_admin_menu' );
    function gowp_admin_menu() {
      global $menu;
      foreach ( $menu as $key => $val ) {
        if ( __( 'Posts') == $val[0] ) {
          $menu[$key][6] = 'dashicons-welcome-add-page';
        }
      }
    }

    Great, thanks Wiktor!

    Hey karpstrucking,

    Thank you for the informations. A question, in which file do I have to paste the print_r( $menu ); to se the output?

    Thank you.

    Moderator bcworkz

    (@bcworkz)

    Ceyar – $menu is a global var, you can put the print_r($menu); on any template file for your theme that’s used for whatever page you’re viewing. I suggest your theme’s footer.php template. Don’t forget to declare global $menu; before you print_r. Be sure it’s all within <?php ?> delimiters.

    I was missing the global $menu before -_-‘ .
    Thank you bcworkz!

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Change Default "Posts" Admin Icon’ is closed to new replies.