• Resolved Andrew Rothman

    (@arothman)


    I’m working on a simple plugin (my first) that converts a given shortcode with a URL as the input parameter into an iframe src. For example [Frame mylink="https://google.com"] This works perfectly, except…

    If the user is working in TinyMCE’s ‘Visual’ mode and copy/pastes the URL from an email into the editor, it often comes along with the <a> tag and turns into a clickable anchor rather than plain text. This breaks the shortcode. I can’t even seem to get the parameter to pass through to my function at all if it’s been wrapped with HTML.

    Is there a way around this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming the anchor tag is properly formed as a shortcode attribute (mainly that internal quotes are properly nested or escaped), the URL should come through to your handler function just fine. You could then use preg_match() to extract a proper URL.

    “Properly formed” is a big assumption. If the quotes aren’t dealt with correctly ( mylink="<a href="https://example.com">link text</a>" ) or the anchor tag is malformed ( no </a> ), the URL is possibly still available, but it will not make it through shortcode_atts(). You’d need to deal with the passed attributes array directly, and search through the attributes for something that looks like an URL with preg_match().

    The alternative is to hook ‘save_post’ and search content for your shortcode. When found, clean up whatever is pasted in by the user by using preg_match() to extract the URL alone and discard anything else along for the ride.

    Thread Starter Andrew Rothman

    (@arothman)

    Yep. The incorrect quotes are exactly what I’m seeing. They’re wrapping a full anchor tag with double-quotes in it around the URL in the shortcode. I’ll look into that preg_match() option and see if that gets me anywhere. I mean, I can easily fix it by just telling the user to un-link the text, but if I can avoid the whole issue, that’ll help!

    Thanks!

    Thread Starter Andrew Rothman

    (@arothman)

    Found a workaround. Rather than having the user put a full URL in the shortcode, we’re just having them enter the latter half of the URL. If there’s no “http” in it, we assume the domain is ours (which it almost always is) and tack it on for them. That will get us around the issues for most implementations.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Removing anchor tag from shortcode input?’ is closed to new replies.