Jquery window.send_to_editor

后端 未结 9 1164
慢半拍i
慢半拍i 2020-12-24 09:22

Couldn\'t come up with a good title for this.

I\'ve got a build in WordPress where I have multiple image uploads using the built in WordPress media uploader. How it\

相关标签:
9条回答
  • 2020-12-24 10:05

    The problem with these solutions is that you won't be able to insert images into posts as the function to do so has been overridden. Here is a modification to the above code that will allow images to still be inserted into the post content through the editor.

    jQuery(document).ready(function() {
    
    var orig_send_to_editor = window.send_to_editor;
    
    jQuery('.upload_image_button').click(function() {
        formfield = jQuery(this).prev('input');
            tb_show('', 'media-upload.php?type=image&TB_iframe=true');
    
            window.send_to_editor = function(html) {
                var regex = /src="(.+?)"/;
                var rslt =html.match(regex);
                var imgurl = rslt[1];
                formfield.val(imgurl);
                tb_remove();
            jQuery('#show_'+formfield.attr('name')).html('<img src="'+imgurl+'" width="150" />')
    
    
                window.send_to_editor = orig_send_to_editor;
            }
    
            return false;
        });
    });
    

    The main difference is that this code saves the original function and reassigns it back to send_to_editor.

    Here is the HTML to go with it as an example:

    <input type="hidden" name="image_1" />
    <?php $post_img = get_post_meta($post->ID,'item_image',true); ?>
    <input class="upload_image_button" type="button" value="<?php echo (empty($post_img)?'Upload Image':'Change Image') ?>" />
    <div id="show_image_1"><?php echo (!empty($post_img)?"<img src='$post_img' width='100' />":'')?></div>
    
    0 讨论(0)
  • 2020-12-24 10:09

    Just need to be able to specify the input field that the value is inserted into.

    var uploadID = ''; /*setup the var*/
    
    jQuery('.upload-button').click(function() {
        uploadID = jQuery(this).prev('input'); /*grab the specific input*/
        formfield = jQuery('.upload').attr('name');
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
    });
    
    window.send_to_editor = function(html) {
        imgurl = jQuery('img',html).attr('src');
        uploadID.val(imgurl); /*assign the value to the input*/
        tb_remove();
    };
    
    0 讨论(0)
  • 2020-12-24 10:09

    Here is my rendition:

    It also allows you to insert PDF's files or anything without a SRC attribute by doing a check for img src and if none, trying href on the document. This one also allows you to use classes and apply this to multiple items.

    The Javascript:

        var formfield = "";
        jQuery('.browse_upload').click(function() {
                formfield = jQuery(this).attr('name');
                tb_show('', 'media-upload.php?type=image&TB_iframe=true');
    
                return false;
        });
        window.send_to_editor = function(html) {
                hrefurl = jQuery('img',html).attr('src');
                if(jQuery(hrefurl).length == 0) {
                    hrefurl = jQuery(html).attr('href'); // We do this to get Links like PDF's
                }
                jQuery('.' + formfield).val(hrefurl);
                tb_remove();
    
        }
    

    Sample HTML

    <input id="option[unique-option]" class="regular-text unique-option" type="text" name="option[unique-option]" value="<?php esc_attr_e( $options['unique-option'] ); ?>" />
    <button class="browse_upload button-secondary" name="unique-option" type="button">Browse</button>
    
    0 讨论(0)
提交回复
热议问题