Skip to content

Instantly share code, notes, and snippets.

@vralle
Last active September 22, 2022 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vralle/b0db1083f1c8e392527e to your computer and use it in GitHub Desktop.
Save vralle/b0db1083f1c8e392527e to your computer and use it in GitHub Desktop.
Image-Shortcake: Fix 'media_send_to_editor' for WP Insert Media
function myTest() {
// Save default function
var defaultSend = wp.media.editor.send.attachment;
// new send function
wp.media.editor.send.attachment = function( props, attachment) {
// If not image, return default function
if ( 'image' !== attachment.type )
return defaultSend( props, attachment)
var caption = attachment.caption,
options, html;
// If captions are disabled, clear the caption.
if ( ! wp.media.view.settings.captions ) {
delete attachment.caption;
}
props = wp.media.string.props( props, attachment );
options = {
id: attachment.id,
post_content: attachment.description,
post_excerpt: caption,
linkto: props.link
};
if ( props.linkUrl ) {
options.url = props.linkUrl;
}
html = wp.media.string.image( props );
// Add linkTo
options.linkto = props.link;
// Remove URL if Link not Custom
if ( 'custom' !== options.linkto && options.url ) {
delete options.url;
}
_.each(
{
align: 'align',
size: 'image-size',
alt: 'image_alt'
}, function( option, prop ) {
if ( props[ prop ] )
options[ option ] = props[ prop ];
}
);
return wp.media.post( 'send-attachment-to-editor', {
nonce: wp.media.view.settings.nonce.sendToEditor,
attachment: options,
html: html,
post_id: wp.media.view.settings.post.id
});
};
};
jQuery(document).ready(function(){
myTest();
});
@goldenapples
Copy link

Nice. Is there a way to do this without overriding this method, or this the only way to get a value for link?

@vralle
Copy link
Author

vralle commented Nov 22, 2015

When in the editor we click "Insert", the function returns an array of options. The code indicates that the options is defined here. You can find the option in the functions below, but the image data is transmitted to "attachment" at start function and not returns. It will need to receive data again.
The example uses the code change only for images. The original function is cached and transmitted as the main, if not the image file. This simplifies support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment