Open media frame and select an attachment

Well, I found the answer myself. I hope it helps others:

I replaced both instances of:

if ( selected ) {
    selection.add( wp.media.attachment( selected ) );
}

with:

selection.reset( selected ? [ wp.media.attachment( selected ) ] : [] );

Apparently, the reset() function can be used to empty an array and then add elements to it, too.

Leave a Comment