Skip to content

Instantly share code, notes, and snippets.

@bradyvercher
Created October 4, 2017 22:30

Revisions

  1. bradyvercher created this gist Oct 4, 2017.
    40 changes: 40 additions & 0 deletions register-plugin-icons.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php
    /**
    * Register plugin icons.
    *
    * WordPress 4.9 introduced icons in the list table on the Manage Plugins
    * screen. The icons are pulled from the W.org update API. If an icon isn't
    * available, a generic plugin Dashicon is shown instead.
    *
    * @param array $value Plugin update data.
    * @return array
    */
    add_filter( 'site_transient_update_plugins', function( $value ) {
    $vendor_slug = 'example'; // Unique vendor slug.
    $plugin_file = 'example/example.php'; // Relative path to the main plugin file from the plugins directory.
    $plugin_icon = 'https://example.com/plugin-icon.png'; // URL to the plugin icon.

    $icons = array(
    '1x' => '',
    '2x' => '',
    'default' => $plugin_icon,
    );

    $plugin_slug = dirname( $plugin_file );

    if ( isset( $value->no_update[ $plugin_file ] ) ) {
    $value->no_update[ $plugin_file ]->icons = $icons;
    } elseif ( isset( $value->response[ $plugin_file ] ) ) {
    $value->response[ $plugin_file ]->icons = $icons;
    } else {
    $data = new stdClass;
    $data->id = $vendor_slug . '/' . $plugin_slug;
    $data->slug = $plugin_slug;
    $data->plugin = $plugin_file;
    $data->icons = $icons;

    $value->no_update[ $plugin_file ] = $data;
    }

    return $value;
    } );