Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Overload wp.media.editor.send to send linkto and linkUrl fields #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions assets/js/image-shortcake-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,51 @@ function updateStringsForImageShortcake() {
attachmentDetailsTwoColumnTemplate.text( newHtml );
};


function overloadMediaSendToEditor() {

if ( 'undefined' === typeof wp ||
'undefined' === typeof wp.media ||
'undefined' === typeof wp.media.editor ||
'undefined' === typeof wp.media.editor.send ||
'undefined' === typeof wp.media.editor.send.attachment ) {
return;
}

// Back up the default function
var defaultSend = wp.media.editor.send.attachment;

// Replace it with this 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);
}

// Back up old wp.media.post and redefine it so as to modify the
// attachment data before sending it.
var oldMediaPost = wp.media.post;

wp.media.post = function( endpoint, postData ) {
wp.media.post = oldMediaPost;

postData.attachment.linkto = props.link;

if ( props.linkUrl && 'custom' === props.link ) {
postData.attachment.url = props.linkUrl;
}

return wp.media.post( endpoint, postData );
};

return defaultSend( props, attachment );
};
};

jQuery(document).ready(function(){
updateStringsForImageShortcake();
overloadMediaSendToEditor();
});

var ImageShortcake = {
Expand Down Expand Up @@ -101,3 +144,4 @@ if ( typeof wp.shortcake !== 'undefined' && typeof wp.shortcake.hooks !== 'undef
wp.shortcake.hooks.addAction( 'img.linkto', ImageShortcake.listeners.linkto );

}

7 changes: 6 additions & 1 deletion inc/class-img-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ public static function filter_media_send_to_editor( $html, $attachment_id, $atta
'image-size' => 'size',
'image_alt' => 'alt',
'post_excerpt' => 'caption',
'width' => 'width',
'linkto' => 'linkto',
'url' => 'url',
);

$shortcode_ui_def = self::get_shortcode_ui_args();
Expand All @@ -380,6 +381,10 @@ public static function filter_media_send_to_editor( $html, $attachment_id, $atta
}
}

if ( ! empty( $shortcode_attrs['linkto'] ) && 'post' === $shortcode_attrs['linkto'] ) {
$shortcode_attrs['linkto'] = 'attachment';
}

/**
* Filter the shortcode attributes when inserting image from the media library.
*
Expand Down