1

I want to add a new class to the <a> that gets generated around an <img> when it's inserted into a post, but only if the link type is set to "Media File".

I see that similar info is available to us in the image_default_link_type setting, where we can specify none, file, post and attachment. So it would seem that these are the keywords used by WordPress.

The only hooks/filters I've found so far are either performed on the "Add/Insert Media" popup itself, or on the content that was generated after being inserted into the post. At this point we usually only have access to the generated URL as a string, and no additional info about its link type.

So is there a way to retrieve the link type and filter the output, either as it's being added to TinyMCE (preferably) or as the post content is being output?

  • I should add, I'm trying to avoid working with the href as a string because there is no foolproof way to figure out exactly which link type was used to generate the URL.
  • If you want to do this at the point of attachment you'll probably need to use the wp.media javascript api – totels Jun 2 '17 at 21:54
  • Thanks @totels, this seems like it would be one way to go. Though it looks like I have a lot of reading ahead, with sparse documentation out there. I've done a bit of digging and it looks like wp.media.events are made globally available, I would have to find out which one fires when the link type is saved... the WordPress Media Backbone Guide plugin is supposed to be a good resource, but even that seems vague. I'm sure it would be a bit easier if I knew Backbone... – LubosB Jun 2 '17 at 23:13
  • indeed, the documentation is quite lacking, there are a decent handful of examples, but most of them only cover "getting started", you may get more value out of finding other plugins that build on top of it and looking at their code. :-/ – totels Jun 3 '17 at 10:15
  • Just to document my progress, I've found the point at which the string to be sent to TinyMCE is created. It's located inside \wptheme\wp-includes\js\media-editor.js wp.media.string = { Basically it's looking at props.link || defaultProps.link || getUserSetting( 'urlbutton', 'file' ) for the values file, embed, post or custom and assigning attachment.url for Media Files and attachment.link for Attachment Pages. The problem is these assignments seem to take place inside anonymous functions, and the stuff that I can log in the console all seems to be minified. – LubosB Jun 3 '17 at 14:23
  • 1
    add define( 'COMPRESS_SCRIPTS', false ); to your wp-config.php, depending on what you end up debugging, you may want define( 'CONCATENATE_SCRIPTS', false ); and define( 'COMPRESS_CSS', false ); too – totels Jun 5 '17 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.