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:
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.