• Resolved pejca

    (@pejca)


    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://ibb.co/6rJNpr3

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The problem with using AI for coding is it doesn’t know any more than a novice coder does about proper syntax and logic. It’s great at cobbling code together from disparate sources that looks perfectly functional, but it doesn’t always work as expected. It would take someone very knowledgeable of coding to suss out what the problems are. With that kind of knowledge, one would not need AI to help them accomplish something. Debugging someone else’s code (AI or human) is always more difficult than debugging one’s own.

    I recommend checking your browser’s console for error messages after you try selecting an image. Such messages will give you a place to start looking for problems. Without such direction, finding code errors is extremely difficult.

    Thread Starter pejca

    (@pejca)

    I know AI isn’t really that great at coding, but I thought it was gonna work because it’s nothing complicated. Anyways, I’ve tried to look for the errors, but there are none. I went to browser console and there were 4 errors (before I clicked it) and when I click nothing changes.

    Thread Starter pejca

    (@pejca)

    I used diffrent AI tool and it helped me lol

    • This reply was modified 1 month, 1 week ago by pejca.
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.