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.
Related Posts:
- Show only images and videos in a wp.media window
- Switch to the library tab in the media uploader
- What type of template are WP media-modal’s templates?
- How to retrieve a value from an input field in a media view template
- Display attachments by ID in a wp.media frame
- Can I use the wp media uploader for my own plugin?
- Customizing the 3.5 “Add Media” popup (Backbone.js)
- Restrict file types in the uploader of a wp.media frame
- Rest API invalid nonce with Backbone Client
- Sending custom data with Media Frame Model
- Only displaying zip files with wp media
- window.send_to_editor throws unterminated string literal error when adding image with multi line description
- Uppload image from another source wp.media
- Multiple media uploader buttons target only one input on the same page
- Setting/unsetting terms using the Backbone JavaScript client
- Is there a media_upload_tabs-similar filter to add tabs to the simple/featured image picker?
- Building a Featured Gallery component for Gutenberg
- How to render WP Rest-API Endpoints in a React.js Theme with Woocommerce
- Check if user can in javascript
- wp-api Backbone JS Client fetch options
- Access checks with custom REST endpoints and backbone
- Insert HTML markup to page content from the Media Frame modal
- Render repeatable widget fields properly with Backbone.js
- Hook into backbone to add js to wp-admin -> media library?
- jQuery append() vs appendChild()
- ChartJS beginAtZero, min, max doesn’t work
- how to fix : ” TypeError: Cannot read property ‘addEventListener’ of null”…?//
- ReferenceError: $ is not defined
- React.js: Set innerHTML vs dangerouslySetInnerHTML
- JS/Ionic:TypeError: Cannot read property ‘code’ of undefined
- Axios Delete request with body and headers?
- React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing
- Change Background color (css property) using Jquery
- jQuery get textarea text
- Base64 encoding and decoding in client-side Javascript
- Response to preflight request doesn’t pass access control check
- Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js
- Get class name using jQuery
- How to clear the canvas for redrawing
- Find an element in DOM based on an attribute value
- character counter – backspace doesn’t reflect on characters remaining
- How does the double exclamation (!!) work in javascript?
- Add days to JavaScript Date
- illegal use of break statement; javascript
- Cannot read property ‘getContext’ of null, using canvas
- How to add multiple classes to a ReactJS Component?
- Make Axios send cookies in its requests automatically
- How to move an element after another element using JS or jquery?
- Converting any string into camel case
- JSON.parse() not working
- Call python function from JS
- How to add onload event to a div element
- Access Control Origin Header error using Axios
- Structs in Javascript
- Refused to execute script, strict MIME type checking is enabled?
- Error: [ng:areq] from angular controller
- getElementsByAttribute() is not a function
- Javascript onload not working
- setInterval with loop time
- JavaScript by reference vs. by value
- navigator.geolocation.getCurrentPosition sometimes works sometimes doesn’t
- Expected ‘this’ to be used by class method
- Strip HTML from Text JavaScript
- How to disable auto-start of “Node.js: Server-side JavaScript” when using Adobe Dreamweaver?
- Regular expression to validate US phone numbers?
- How should I initialize jQuery?
- How to reload page every 5 seconds?
- WordPress Bootstrap Handburger Menu Wont Open
- Set default image link target in Gutenberg image block
- Stop WordPress from reserving slugs for media items?
- How do I detach images from posts?
- Get loading state of wp data selector
- Filter medias from the Media Uploader (wp.media) modal with a post meta
- Adding a text element in between title and blocks container
- Block Editor: How to get title and post content within the WordPress admin UI in Javascript?
- Using wp_localize_script inside template page to transfer var from php to js
- Handling image uploads without thickbox
- How to return div with custom attributes(Coding a Gutenberg Block)
- Problem with JavaScripts addition in WordPress
- Setting a menu link to open a pop-up
- How to display post content in the block editor
- My Button Default Click Is Not Working In WordPress Using Javascript Code
- WordPress update_user_meta onclick button with Ajax
- Javascript to open new window
- Add a Page Screen is Visually Blank
- Debugging JS errors
- Javascript File Does Not Work
- Gutenberg sidebar show input field on toggle
- Shortcode for Custom Field of Media Attachment (to use with Featured Images)
- Fade-in Fade-out doesn’t work in WordPress
- Is it feasible to call angular’s page from wordpress website?
- Connecting a wordpress site to an AngularJS APP
- Get posts associated with media attachment
- WOW.js Script Tag Initialization Header/Footer
- Making the HTML list to a checkbox tree with the plugin jstree [closed]
- WordPress Gutenberg: Attribute overwritten by block duplicate
- I have custom html mixed with inline javascript that I want added to my WordPress site, how do I achieve this?
- How to update the input[type=number] quantity field using custom buttons?
- How to properly move media files and update data?
- Why isn’t my SVG path rotating on scroll on iPhones?