Add media count in Media Gallery

The following code will display a count of the number of media in every user’s Media Gallery, as shown below:

Once the code is added, you can also include the show_count="true" attribute in the rtMedia Gallery Shortcode to display media count in galleries created via shortcode:

[rtmedia_gallery global="true" show_count="true"]

Paste the following code into your functions.php file:

/**
 * Add filter to modify media query.
 */
function my_rtmedia_set_query_filters() {
    add_filter( 'rtmedia_media_query', 'my_modify_media_query', 9, 3 );
}
add_action( 'rtmedia_set_query', 'my_rtmedia_set_query_filters', 99 );

/**
 * Modifies the media query.
 *
 * @param  array $media_query  Refer the `rtmedia_media_query` filter defined in the rtMedia plugin
 * @param  array $action_query
 * @param  array $query
 *
 * @return array $media_query
 */
function my_modify_media_query( $media_query, $action_query, $query ) {
    global $rtmedia_query, $media_query_clone_ids;

    // Store the `media_name` parameter to be used in the rtmedia-model-where-query filter
    $media_query_clone_ids = $media_query;

    if ( isset( $media_query['show_count'] ) ) {
        if ( ! empty( $media_query['show_count'] ) ) {
            // unset from global query so that multiple gallery shortcode can work
            if ( isset( $rtmedia_query->query ) && isset( $rtmedia_query->query['show_count'] ) ) {
                unset( $rtmedia_query->query['show_count'] );
            }
        }
        // unset it, so that it wont affect the other rtmedia_gallery shortcodes on the same page
        unset( $media_query['show_count'] );
    }
    return $media_query;
}

/**
 * Show media count in rtMedia gallery shortcode when show_count parameter is set to true.
 */
function rtmedia_gallery_after_title_add_media_count() {
    global $rtmedia_query;

    // Check for gallery shortcode.
    if ( isset( $rtmedia_query->is_gallery_shortcode ) && true === $rtmedia_query->is_gallery_shortcode ) {
        global $rtmedia_shortcode_attr;

        // check in the gallery shortcode $show_count is set to true.
        if ( isset( $rtmedia_shortcode_attr['show_count'] ) && 'true' === (string) $rtmedia_shortcode_attr['show_count'] ) {
            // Defalut value for media count.
            $media_count = 0;
            if ( isset( $rtmedia_query->is_gallery_shortcode ) ) {
                // get value from the pagination.
                $media_count = intval( $rtmedia_query->media_count );
            }
            ?>
            <h2 class="rtm-gallery-count">
                <?php
                $total_text = esc_html__( 'Total media:', 'buddypress-media' );

                // Filter for the counter text.
                echo apply_filters( 'rtmedia_total_counter_text', $total_text );
                echo sprintf( ' %d ', intval( $media_count ) );
                ?>
            </h2>
            <?php
        }
    }
}
add_action( 'rtmedia_album_gallery_actions', 'rtmedia_gallery_after_title_add_media_count', 10 );
add_action( 'rtmedia_media_gallery_shortcode_actions', 'rtmedia_gallery_after_title_add_media_count', 99 );