• pejca

    (@pejca)


    Hey,
    I am making a simple plugin using https://zzzcode.ai/ because I’m bad at php and js.
    The plugin requires you to choose an image in the wp media library. When I click on the button and open media library, it’s empty. Is it something with permissions or what?

    This is my js:

    jQuery(document).ready(function($) {

        $('#upload_image_button').click(function(e) {

            e.preventDefault();

            var image_frame;

            if (image_frame) {

                image_frame.open();

                return;

            }

            image_frame = wp.media({

                title: 'Select Image',

                button: {

                    text: 'Use this image'

                },

                multiple: false

            });

            image_frame.on('select', function() {

                var attachment = image_frame.state().get('selection').first().toJSON();

                $('.image-upload').val(attachment.url);

            });

            image_frame.open();

        });

    });
Viewing 10 replies - 1 through 10 (of 10 total)
  • threadi

    (@threadi)

    What does your PHP code look like? My guess is that you are trying to execute this JavaScript code on a page where no media library JS libraries are loaded. To do this, you must also execute https://developer.www.ads-software.com/reference/functions/wp_enqueue_media/ on this page.

    Thread Starter pejca

    (@pejca)

    function load_media_files() {

    ? ? wp_enqueue_media();

    }

    add_action( 'admin_enqueue_scripts', 'load_media_files' );

    function cep_create_menu() {

    ? ? add_menu_page('Create Collection', 'Create Collection', 'manage_options', 'create-collection', 'cep_create_collection_page');

    }

    add_action('admin_menu', 'cep_create_menu');

    function cep_create_collection_page() {

    ? ? ?>

    ? ? <div class="wrap">

    ? ? ?some unimportant code...

    ? ? </div>

    ? ? <?php

    ? ? if (isset($_POST['submit'])) {

    ? ? ? ? cep_create_elementor_page($_POST['name'], $_POST['tag_slug'], $_POST['image']);

    ? ? }

    }

    function cep_create_elementor_page($name, $tag_slug, $image) {

    ? ? $page_id = wp_insert_post(array(

    ? ? ? ? 'post_title' => $name,

    ? ? ? ? 'post_content' => '[products tag="' . esc_attr($tag_slug) . '"]',

    ? ? ? ? 'post_status' => 'publish',

    ? ? ? ? 'post_type' => 'page',

    ? ? ));

    ? ? if ($page_id) {

    ? ? ? ? update_post_meta($page_id, '_elementor_data', json_encode(array(

    ? ? ? ? ? ? 'version' => '3.0.0',

    ? ? ? ? ? ? 'elements' => array(

    ? ? ? ? ? ? ? ? array(

    ? ? ? ? ? ? ? ? ? ? 'id' => 'image',

    ? ? ? ? ? ? ? ? ? ? 'elType' => 'widget',

    ? ? ? ? ? ? ? ? ? ? 'widgetType' => 'image',

    ? ? ? ? ? ? ? ? ? ? 'settings' => array(

    ? ? ? ? ? ? ? ? ? ? ? ? 'image' => $image,

    ? ? ? ? ? ? ? ? ? ? ? ? 'image_size' => 'full',

    ? ? ? ? ? ? ? ? ? ? ? ? 'width' => '1200',

    ? ? ? ? ? ? ? ? ? ? ? ? 'height' => '540',

    ? ? ? ? ? ? ? ? ? ? ),

    ? ? ? ? ? ? ? ? ),

    ? ? ? ? ? ? ? ? array(

    ? ? ? ? ? ? ? ? ? ? 'id' => 'spacer',

    ? ? ? ? ? ? ? ? ? ? 'elType' => 'widget',

    ? ? ? ? ? ? ? ? ? ? 'widgetType' => 'spacer',

    ? ? ? ? ? ? ? ? ? ? 'settings' => array(

    ? ? ? ? ? ? ? ? ? ? ? ? 'height' => '50',

    ? ? ? ? ? ? ? ? ? ? ),

    ? ? ? ? ? ? ? ? ),

    ? ? ? ? ? ? ),

    ? ? ? ? )));

    ? ? }

    }

    function cep_enqueue_media() {

    ? ? wp_enqueue_media();

    ? ? wp_enqueue_script('cep-media-upload', plugin_dir_url(__FILE__) . 'media-upload.js', array('jquery'));

    }

    add_action('admin_enqueue_scripts', 'cep_enqueue_media');

    This is almost full php file.

    So Im not sure what do i do with wp_enqueue_media( array $args = array() ) , do i just replace that with already existing wp_enqueue_media() ? (I’m probably being dump right now, sorry)

    Thread Starter pejca

    (@pejca)

    I tried to replace wp_enqueue_media() and it stopped my site from working. Something about fatal error. After I activated my backup from few days ago I try it again and it didnt let me activate the plugin.

    Plugin could not be activated because it triggered a?fatal error.

    So i take it as I wasn’t supposed to do that.

    threadi

    (@threadi)

    The code works for me without any problems. I have added a button to ‘some unimportant code…’ which triggers your JavaScript:

    <a href="#" id="upload_image_button">button</a>

    When I click on it, the media library loads and I can also see files there. I can also add new files.

    If this doesn’t happen with you, it could be that some other script you have is interfering. Deactivate all plugins and switch to a standard theme like TwentyTwentyFour.

    If that doesn’t work either, check the JavaScript console to see if you see any errors when you click on the button.

    Thread Starter pejca

    (@pejca)

    Well, it looks like it wasn’t ‘some unimportant code’. I added you knew button next to my old button (<button class="button" id="upload_image_button">Upload Image</button>) and your does nothing somehow. I changed the theme to default and it’s the same.

    Thread Starter pejca

    (@pejca)

    I just made it kind of work…

    I go to my home page of the website and on the top of the page i see “Collections #Custom Elementor Page Maker” (or something similar). I go to delete the plugin and it says it’s deleted. I go to my FTP terminal and I see the folder is still there so I delete it. I go back to browser and go to my tab where plugin is loaded and still not refreshed(https://ibb.co/8MqvHYQ). For some reason I click on the upload image button and… it works? I refresh the page and obviously it says the page doesn’t exist. Now I upload it again and again it isn’t working.

    Thread Starter pejca

    (@pejca)

    Just reinstalled and deleted the folder via terminal (again) and i clicked on the button: https://ibb.co/bFXkxnW

    • This reply was modified 3 months ago by pejca.
    threadi

    (@threadi)

    I would still recommend that you really deactivate all plugins as long as you are developing your own plugins.

    Thread Starter pejca

    (@pejca)

    I deactivated all the plugins and it’s still not working.

    threadi

    (@threadi)

    What does “not working” mean exactly? What is happening or not happening? It works fine for me with the above code. I can open the library multiple times and close it again.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.