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?
- Get a default customizeAction text for a section using Customizer JS API
- Dynamically write in editor with Javascript
- Redirect to homepage if attmpting to leave intranet [closed]
- Why does my media selection script write to ONLY the first input field?
- Taxinomy slug as var in parent archive page, without having a specific taxinomy page
- Twenty seventeen theme dropdown menu issue
- How to verify that a field has a value before adding a term to a taxonomy
- Including js script at end of header
- Where is fusion-gmap-container?
- Dynamically changing logo on scroll function [closed]
- Escaping quotes while enqueuing scripts
- Using the GetMediaLibrary() method in python-wordpress-xmlrpc.media, how to iterate over the result?
- The page isn’t redirecting properly was given when redirecting
- How To Pull Information From A Page And Input The Data In The WordPress User Database Table
- How to register or enqueue script and stop it being called in head?
- How to correctly load this jquery script through the file functions.php?
- contact form 7 javascript and wpcf7_before_send_mail action
- Adding javascript script to header via functions.php
- Variations Javascript not working when product display on Quick view via ajax
- adding script tag in head of specific pages
- How to use value from modal tinymce windowManager?
- how to refresh page after user logout with ‘wp_logout_url’?
- Open image gallery on link click
- How do I call for two js files into a custom template?
- How to delay display of page elements until enqueued script has injected html
- load-scripts.php loads incorrect file names
- How to change number field to text field using JS
- Enqueue concatinated JS file in WordPress
- Check if author or uploader id of the attachment(uploaded) image is match?
- Custom Media Meta fields to alter the generated image HTML that is inserted into a post for my animated GIF image player?
- wp_enqueue_scripts doesn’t work for template pages
- WordPress text widget with onclick function
- Create new product with woocommerce REST API with javascript (clientside)?
- Limit attachment caption characters
- Connecting a wordpress site to an AngularJS APP
- Can’t change javascript files
- how to do open a link in a open window from menu bar? [closed]
- Get posts associated with media attachment
- How to Insert images in WordPress using code
- Combining results from WP-API using AngularJS
- JavaScript and Google PageSpeed + wp_enqueue_script
- Javascript file doesn’t load
- Script not working in post content
- JavaScript problem in a WordPress PAGE
- How to identify which javascript is being executed
- Put dynamic Javascript in header after doing operations
- Unserialize WP_Options options programatically?
- how to include js in widget?
- early enqueueing javascript file in page template, not in functions.php
- Wysiwyg editor not working on 4.2.2
- Editing the source of a display for posts (to category recent)
- Dequeue set-post-thumbnail.min.js
- How to adjust the selector used in JS to target only the one that was clicked on?
- WordPress 4.2 mce-views migration guide?
- Receive “menu-toogle” event from section menu in customizer
- Adding javascript blocks to a single file and adding it to the header
- None of the JavaScript works when using wp_editor
- How to retrieve images attached to post through media library?
- How would I get this to work – send to post from thick box
- JS files landing on page but not working, using childtheme of twentyeleven
- WordPress 4.1 crashes loading of ArcGIS Javascript API
- jQuery + more won’t load in header
- controlling whether upload is attached to post or not
- How to modify a class in load-scripts.js ?
- Remove attached media
- Convert javascript running xml files to wordpress platform
- WOW.js Script Tag Initialization Header/Footer
- Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
- Remove WordPress default registration client side validation?
- How to integrate a different (JavaScript) editor in WordPress?
- JS enqueue path (localhost)
- Load and execute javascript from body
- Why can’t I load JS script in a plugin?
- Page template dynamic links based on browser size
- javascript errors on mobile browser, not on desktop
- Trigger a function during onload