Help me open media library with my plugin.

Hey,

I have almost no knowlage of javascript or php, but I know some python so I am begginer.

I wanted to make simple plugin and I asked ChatpGPT for help. It was all grate until we crossed this problem where wordpress media library won't open.

php:

function my_enqueue_media_uploader() {
    wp_enqueue_media();
   }
   add_action('admin_enqueue_scripts', 'my_enqueue_media_uploader');

function collections_admin_scripts($hook) {
    if ($hook != 'products_page_collections') {
        return;
    }
    wp_enqueue_media(); // Ensure media library scripts are loaded
    wp_enqueue_script('collections-admin-script', plugins_url('/js/admin.js', __FILE__), array('jquery'), null, true);
}

js:

jQuery(document).ready(function($) {
    var frame;

    $('#select_image_button').on('click', function(e) {
        e.preventDefault();

        // If the media frame already exists, reopen it.
        if (frame) {
            frame.open();
            return;
        }

        // Create a new media frame
        frame = wp.media({
            title: 'Select Image',
            button: {
                text: 'Use this image'
            },
            multiple: false
        });

        // When an image is selected, run a callback.
        frame.on('select', function() {
            var attachment = frame.state().get('selection').first().toJSON();
            $('#picture_id').val(attachment.id);
            $('#image_preview').html('<img src="' + attachment.url + '" style="max-width: 200px; max-height: 200px;">');
        });

        // Finally, open the modal
        frame.open();
    });
});

This is code ChatGPT wrote me and this is what it looks like:

https://preview.redd.it/rhgp2viixgkd1.png?width=690&format=png&auto=webp&s=acd5c312d372e0136adb0ee4991fe1f7222c0fd0

The only problem is, Select Image isn't doing anything. I've tried searching on forums but i haven't found anything that helped so I came here. Anything can help! Thanks in advance.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer