wp.media multiple selection limit

As far as I know, that feature is not officially supported, so there is no easy way to achieve it.

But if you must, then it can be made possible by extending the wp.media.model.Selection.prototype.add function which is used to add new items to the selection list.

Working example: (tried & tested with WordPress v6.2.2)

( You can see the original function/code here or find it in wp-includes/js/media-models.js. )

wp.media.model.Selection.prototype.add = function ( models, options ) {
    if ( ! this.multiple ) {
        this.remove( this.models );
    } else {
        const maxItems = parseInt( this.multiple, 10 );

        if ( maxItems >= 1 ) {
            if ( this.length >= maxItems ) {
                this.set( this.slice( 0, maxItems ) );
                models = []; // Cancel the selection.
                //console.log( 'single selection (via Ctrl key) sliced' );
            } else if ( models?.length > maxItems ) {
                models = models.slice( 0, maxItems );
                //console.log( 'multi selection (via Shift key) sliced' );
            }
        }
    }

    return wp.media.model.Attachments.prototype.add.call( this, models, options );
}

How to use the script:

  1. Make sure it runs only after the WordPress media models (handle name: media-models) are loaded. So basically, you can run that script right before you call wp.media(), or you can instead run the script once on page load, but as for how, that is up to you.

  2. Just set the multiple to the maximum number of items that can be selected via the dialog.

    So for example, instead of multiple: true, you would use multiple: 5 to limit the selection to at most 5 items.

deneme bonusu veren sitelerbahis siteleripulibet girişdeneme bonusutürkçe altyazılı pornocanlı bahis casinocanlı bahis casino siteleriOnwin Güncel Girişholiganbetholiganbet girişholiganbet güncel giriş