1

When viewing the Favorite Plugins tab, /wp-admin/plugin-install.php?tab=favorites, the list comes unordered, is it possible to sort it from A to Z?

favorite plugins screen

1 Answer 1

3

That page is rendered by the WP_Plugin_Install_List_Table class, which calls the function plugins_api().
In this function, we find two filter hooks that enable the manipulation of the table output: plugins_api_args and plugins_api_result.

The problem is that the API doesn't offer an ordered query, so we can only sort each page results. The solution is to increase the number of items per page until no paging is necessary. The default limit is 30 plugins.

Related Q&A:

Relevant links:


The following plugin sorts the list by a given argument (name, requires and others, from the Plugin API response).

<?php
/*
Plugin Name: All my Favorites
Plugin URI:  https://wordpress.stackexchange.com/q/76643/12615
Description: Order and increment the number of plugins per page in the Favorites tab.
Version:     1.0
Author:      Rodolfo Buaiz
Author URI:  https://wordpress.stackexchange.com/users/12615/brasofilo
License:     GPL v2
*/

!defined( 'ABSPATH' ) AND exit();

if ( !class_exists( 'All_Favorites_Ordered' ) ) :

class All_Favorites_Ordered 
{
    // store the options
    protected $params;

    /**
     * Set up plugin
     * 
     * @param  array $options  [per_page, order_by, order]
     * @return void
     */
    public function __construct( array $options ) 
    {
        $this->params = $options;    
        add_filter( 'plugins_api_args', array( $this, 'max_num_of_plugs' ), 10, 2 );
        add_filter( 'plugins_api_result', array( $this, 'order_plugins' ), 10, 3 );
    }   

    /**
     * Modifies the per_page argument when querying Favorites
     * 
     * @param  object $args [page, per_page, browse/user]
     * @param  object $action [query_plugins]
     * @return object $args
     */
    public function max_num_of_plugs( $args, $action )
    {
        // Not our query, exit earlier. 
        // Other tabs have browse instead of user.
        if( !$args->user ) 
            return $args;

        $args->per_page = $this->params['per_page'];
        return $args;
    }

    /**
     * Sort result from Plugin API call
     * Add admin head action to print CSS
     * 
     * @param  object $res API response
     * @param  string $action [query_plugins]
     * @param  object $args [per_page, order_by, order]
     * @return object $res Original or modified response
     */
    public function order_plugins( $res, $action, $args )
    {
        // Not our query, exit earlier
        if( !$args->user ) 
            return $res;

        // Amazingly, this works here
        add_action( 'admin_head-plugin-install.php', array( $this, 'hide_pagination' ) );

        usort( $res->plugins, array($this, 'sort_obj') );

        return $res;
    }

    /**
     * Hide Paging and other elements from Favorites screen
     * 
     * @return string Echoed in admin_head
     */
    public function hide_pagination()
    {
        echo '<style>.install-help, .tablenav.top, .tablenav.bottom {display:none !important}</style>';
    }    

    /**
     * Sort array of objects
     * 
     * @param  int callback ( mixed $a, mixed $b )
     * @return object Sorted array
     */
    private function sort_obj( $a, $b ) 
    { 
        $val = $this->params['order_by'];

        if( 'ASC' == $this->params['order'] )
            return strnatcmp( strtolower($a->$val), strtolower($b->$val) );
        else
            return strnatcmp( strtolower($b->$val), strtolower($a->$val) );
    }

}
endif;

/**
 * Initialize the plugin.
 * Possible order_by: name, slug, requires, tested (up to), rating, num_ratings, version
 */
new All_Favorites_Ordered( array(
        'per_page'  => 200
    ,   'order_by'  => 'name'
    ,   'order'     => 'ASC'
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.