/* * Attaches the image uploader to the input field * Author: Amal Amrani * Author URI: http://amrani.es */ jQuery(document).ready(function($){ // Instantiates the variable that holds the media library frame. var meta_image_frame; // Runs when the image button is clicked. $('#meta-image-button').click(function(e){ // Prevents the default action from occuring. e.preventDefault(); // If the frame already exists, re-open it. if ( meta_image_frame ) { meta_image_frame.open(); return; } // Sets up the media library frame meta_image_frame = wp.media.frames.meta_image_frame = wp.media({ title: meta_image.title, button: { text: meta_image.button }, library: { type: 'image' } }); // Runs when an image is selected. meta_image_frame.on('select', function(){ // Grabs the attachment selection and creates a JSON representation of the model. var media_attachment = meta_image_frame.state().get('selection').first().toJSON(); // Sends the attachment URL to our custom image input field. $('#meta-image-header').val(media_attachment.url); /////////// var urlCurrentImage = $('#meta-image-header').val(); //console.log(urlCurrentImage); if ( urlCurrentImage.length ){ $('.meta-image-preview').attr("src", urlCurrentImage); $('.clearImage').css('display','block'); $('.wrap-meta-image-preview').css('display','block'); } }); // Opens the media library frame. meta_image_frame.open(); }); // To delete current image $('.clearImage').click(function(){ $('#meta-image-header').val(''); $('.wrap-meta-image-preview').css('display','none'); $(this).css('display','none'); }); // open library to select marker now var meta_image_frame2; // Runs when the image button is clicked. $('#meta-image-marker').click(function(e2){ // Prevents the default action from occuring. e2.preventDefault(); // If the frame already exists, re-open it. if ( meta_image_frame2 ) { meta_image_frame2.open(); return; } // Sets up the media library frame meta_image_frame2 = wp.media.frames.meta_image_frame2 = wp.media({ title: meta_image.title, button: { text: meta_image.button }, library: { type: 'image' } }); // Runs when an image is selected. meta_image_frame2.on('select', function(){ // Grabs the attachment selection and creates a JSON representation of the model. var media_attachment = meta_image_frame2.state().get('selection').first().toJSON(); // Sends the attachment URL to our custom image input field. $('#icon-marker-metabox-id-url').val(media_attachment.url); var urlCurrentImage = $('#icon-marker-metabox-id-url').val(); if ( urlCurrentImage.length ){ $('.meta-image-preview2').attr("src", urlCurrentImage); $('.wrap-meta-image-preview2').css('display','block'); $('#icon-marker-metabox-id-url').val(urlCurrentImage); $('.clearMarkerIcon').css('display','block'); } }); // Opens the media library frame. meta_image_frame2.open(); }); $('.clearMarkerIcon').click(function(){ $('#icon-marker-metabox-id-url').val(''); $('.wrap-meta-image-preview2').css('display','none'); $(this).css('display','none'); }); });