Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Last active December 6, 2016 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingram3541/65927590d1df05e933a9 to your computer and use it in GitHub Desktop.
Save pingram3541/65927590d1df05e933a9 to your computer and use it in GitHub Desktop.
BeTheme Muffin Options - Upload Field - Src Path to Relative URL
function MfnUpload(){
(function($) {
jQuery( 'img[src=""]' ).attr( 'src', mfn_upload.url );
jQuery( '.mfn-opts-upload' ).click( function( event ) {
event.preventDefault();
var activeFileUploadContext = jQuery( this ).parent();
var type = jQuery( 'input', activeFileUploadContext ).attr( 'class' );
custom_file_frame = null;
// Create the media frame.
custom_file_frame = wp.media.frames.customHeader = wp.media({
title: jQuery(this).data( 'choose' ),
library: {
type: type
},
button: {
text: jQuery(this).data( 'update' )
}
});
custom_file_frame.on( "select", function() {
var attachment = custom_file_frame.state().get( "selection" ).first();
//convert url to relative path
var el = document.createElement('a');
el.href = attachment.attributes.url; //use el.pathname
// Update value of the targetfield input with the attachment url.
jQuery( '.mfn-opts-screenshot', activeFileUploadContext ).attr( 'src', el.pathname );
jQuery( 'input', activeFileUploadContext )
.val( el.pathname )
.trigger( 'change' );
jQuery( '.mfn-opts-upload', activeFileUploadContext ).hide();
jQuery( '.mfn-opts-screenshot', activeFileUploadContext ).show();
jQuery( '.mfn-opts-upload-remove', activeFileUploadContext ).show();
});
custom_file_frame.open();
});
jQuery( '.mfn-opts-upload-remove' ).click( function( event ) {
event.preventDefault();
var activeFileUploadContext = jQuery( this ).parent();
jQuery( 'input', activeFileUploadContext ).val('');
jQuery( this ).prev().fadeIn( 'slow' );
jQuery( '.mfn-opts-screenshot', activeFileUploadContext ).fadeOut( 'slow' );
jQuery( this ).fadeOut( 'slow' );
});
})(jQuery);
}
jQuery(document).ready(function($){
var mfn_upload = new MfnUpload();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment