admin管理员组

文章数量:1195956

Here in a JS class, I used "wp.media" to upload images. I can open the media popup by clicking a button. But getting this error (Uncaught TypeError: Cannot read properties of undefined (reading 'state')) while submitting the image.

    constructor ( btn ) {
        super();
        this.uploaderBtn = $( btn );
        this.mediaFrame;

        this.events();
    }

    events () {
        this.uploaderBtn.on( 'click', this.upload.bind( this ) );
    }

    upload ( e ) {
        e.preventDefault();
        //If the uploader object has already been created, reopen the dialog
        if ( this.mediaFrame ) {
          this.mediaFrame.open();
          return;
        }

        //Extend the wp.media object
        this.mediaFrame = wp.media.frames.file_frame = wp.media({
          title: 'Choose Image',
          button: {
            text: 'Choose Image'
          },
          multiple: true
        });

        this.mediaFrame.on('select', function() {
            const selection = this.mediaFrame.state().get('selection');
            console.log( selection );
        });

        this.mediaFrame.open();
    }

本文标签: plugin developmentwpmedia Uncaught TypeError Cannot read properties of undefined (reading 39state39)