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

Conversation

goldenapples
Copy link
Contributor

Based off code by @vralle at https://gist.github.com/vralle/b0db1083f1c8e392527e

I wanted a way to tap into the wp.media.editor.send.attachment functionality which didn't completely break the connection to core's functionality. This is the best I could come up with for now - temporarily redefining the ajax method so that the post data can be modified before posting to admin-ajax.

The benefits of this approach over just copy-pasting the wp.media.editor.send.attachment functionality from core are that:

  • we're not preventing other plugins from also tapping into this method
  • we don't cut ourselves off from core's functionality, so that if this method is updated in core, we still get to use the core function

See #65

Based off code by @vralle at https://gist.github.com/vralle/b0db1083f1c8e392527e

I wanted a way to tap into the `wp.media.editor.send.attachment`
functionality which didn't completely break the connection to core's
functionality. This is the best I could come up with for now -
temporarily redefining the ajax method so that the post data can be
modified before posting to admin-ajax.

The benefits of this approach over just copy-pasting the
`wp.media.editor.send.attachment` functionality from core are that:
* we're not preventing other plugins from also tapping into this method
* we don't cut ourselves off from core's functionality, so that if this
  method is updated in core, we still get to use the core function; just
  temporarily interrupted at the stage of posting.
In the media editor, `linkto="post"` is the value for "Link to Attachment
Page". In the shortcode UI, we call that value `linkto="attachment"`.
This converts the values that might be returned by media-send-to-editor
to the values we want to use in the shortcode. Props @vralle.
Each line in an array declaration must end in a comma.
@goldenapples
Copy link
Contributor Author

@vralle I'd appreciate thoughts on this approach. It basically does the same thing as the gist you shared in #65, but without the copy-pasted code from core.

I think something along these lines is necessary to allow developers to add their own customization into the method - in my theme, I have another method elsewhere already overwriting wp.media.editor.send.attachment to add other customizations to the image values, and the approach you started with doesn't allow for chaining of functions in one hook like that.

Overloading a basic method like wp.media.post seems awful and dangerous, but I couldn't see another way to tap into this method. (This is another place where WP JS hooks would be really useful.)

@vralle
Copy link

vralle commented Dec 3, 2015

The perfect solution !
I can not imagine a situation where the proposed code can be dangerous.
An exception may be private customization code, damaging WP default functional: responsibility for this falls on the code developers.

@vralle
Copy link

vralle commented Dec 5, 2015

And other way:

if( !empty( $shortcode_attrs['url'] ) ) {
        if ( $shortcode_attrs['url'] == get_attachment_link( $attachment_id ) ) {
            $shortcode_attrs['linkto'] = 'post';
            unset( $shortcode_attrs['url'] );
        } elseif ( $shortcode_attrs['url'] == wp_get_attachment_url( $attachment_id ) ) {
            $shortcode_attrs['linkto'] = 'file';
            unset( $shortcode_attrs['url'] );
        } else {
            $shortcode_attrs['linkto'] = 'custom';
        }
    }

    $shortcode_attrs = array_filter($shortcode_attrs);

This fragment of 'filter_media_send_to_editor'

@montchr
Copy link
Contributor

montchr commented Feb 16, 2016

When I check this out, spaces in caption and alt fields are encoded to %20.

On master:

[img attachment="27274" align="alignleft" size="medium" alt="Donald Trump speaking at the 2013 Conservative Political Action Conference." caption="Donald Trump speaking at the 2013 Conservative Political Action Conference." credit="Photo by Gage Skidmore via Flickr" /]

On this branch:

[img attachment="27274" align="alignleft" size="medium" alt="Donald%20Trump%20speaking%20at%20the%202013%20Conservative%20Political%20Action%20Conference." caption="Donald%20Trump%20speaking%20at%20the%202013%20Conservative%20Political%20Action%20Conference." url="http://bp-staging.dev/app/uploads/2015/11/8566730507_15a8b91f08_k.jpg" credit="Photo by Gage Skidmore via Flickr" /]

@goldenapples
Copy link
Contributor Author

@montchr That shouldn't be changing specifically on this branch.

Url-encoding of caption & alt fields is intentional, and was added in #67 to allow for quotes and special characters to be used in captions and alt fields.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants