How to transfer value from Wordpress Thickbox?

I am currently working on a custom Wordpress plugin that requires the user to create a list in the form, and therefore, to help them fill out their list, I have implemented Wordpress Thickbox. I did a Thickbox mapping with the content that I would like, however what I am trying my best to do is return the data to its original form.

The form of the original looks like this:

<input name="input_that_wants_data" id="input_for_data" type="text" />
<a href="#TB_inline?width=600&height=550&inlineId=my-content-id" class="thickbox">Click Here for Modal</a>

As expected, any form. Basically, I want the information from the modal object to pass my string back to input_for_data p>

In the code inside the module there are several rows of the table, for example:

<td><input type="checkbox" class="modal_checkbox_class" value="'.$data->value.'"></td>

Basically, what I would like to do is build an array of values โ€‹โ€‹for each clicked checkbox, and then use the split Javascript function to turn it into a string that I would return to an input field outside of modality.

Any help is appreciated. I would prefer a Javascript / jQuery solution for this

+4
source share
1 answer

I used this tutorial to do something you need: https://code.tutsplus.com/tutorials/getting-started-with-the-wordpress-media-uploader--cms-22011

My code is as follows:

    function renderMediaUploader() {
    'use strict';

    var file_frame, image_data;

    /**
     * If an instance of file_frame already exists, then we can open it
     * rather than creating a new instance.
     */
    if ( undefined !== file_frame ) {

        file_frame.open();
        return;

    }

    /**
     * If we're this far, then an instance does not exist, so we need to
     * create our own.
     *
     * Here, use the wp.media library to define the settings of the Media
     * Uploader. We're opting to use the 'post' frame which is a template
     * defined in WordPress core and are initializing the file frame
     * with the 'insert' state.
     *
     * We're also not allowing the user to select more than one image.
     */
    file_frame = wp.media.frames.file_frame = wp.media({
         title: 'Select or Upload Media Of Your Chosen Persuasion',
          button: {
            text: 'Use this media'
          },
        multiple: true
    });

    //add items from thickbox to table
    file_frame.on( 'select', function() {

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

        jQuery.each(attachment, function(i, val){
            jQuery('table').show();
            jQuery('table tbody').append('<tr class="table_row"><td class="col-sm-2"><img class="img-responsive" src="'+val.url+'"></td><td class="col-sm-8"><input style=" display: block;" type="text" name="entry[url][]" value="'+ val.url +'"></td></tr>');
        });


    });

    // Now display the actual file_frame
    file_frame.open();

}

(function( $ ) {
    'use strict';

    $(function() {
        $( '#set-footer-thumbnail' ).on( 'click', function( evt ) {

            // Stop the anchor default behavior
            evt.preventDefault();

            // Display the media uploader
            renderMediaUploader();

        });

    });

})( jQuery );
+2
source

Source: https://habr.com/ru/post/1661222/


All Articles