0

Hi I am trying to add a url to an image tag with a wordpress shortcode that I already have... How can I get it to work?

1
  • I'm not sure you can do it that way? You'd probably have to create an image tag shortcode, can you provide some context as to what problem this is trying to solve? There's probably a far better way of doing it
    – Tom J Nowell
    Nov 27 '17 at 17:59
1

You cannot use Shortcodes in attributes since 4.2.3, for security reasons. See the announcement (emphasis mine):

Earlier today, we released WordPress 4.2.3, which includes a relatively large security fix that affects the Shortcode API. Due to the nature of the fix – as is often the case with security fixes – we were unable to alert plugin authors ahead of time, however we did make efforts to scan the plugin directory for plugins that may have been affected.

With this change, every effort has been made to preserve all of the core features of the Shortcode API. That said, there are some new limitations that affect some rare uses of shortcodes.

...

In today’s release of WordPress 4.2.3, however, we’ve added some new limitations that affect some existing plugins. Take, for example, the following shortcode, which is no longer recognized:

<div style="background-image: url('[shortcode]');">

The shortcode in the example above appears in a context that is no longer supported. Further, this use of a shortcode stretches the imagination for how the Shortcode API was intended to be used. Fortunately, there are some workarounds still available, so that site administrators are not overly restricted in their use of HTML.

Read the full announcement for the workarounds.

0

This should work:

<img src="<?php echo do_shortcode('[my_shortcode]'); ?>" />
5
  • Hi, I am using this wordpress plugin... wordpress.org/plugins/url-params .... How can I add this shortcode for the image.... <img src="[urlparam param="profile_pic" /]" /> .... url is example.com/?profilepic=https://upload.wikimedia.org/wikipedia/… Nov 27 '17 at 21:11
  • You must replace your shortcode with the one in the example, so basically you would get <img src="<?php echo do_shortcode('[urlparam param="profile_pic"]'); ?>" />. If you want to take into account the security issue pointed out in the other answer, I guess you could just store the string returned by the shortcode into a variable, and then use it in the "src" attribute: <?php $src = do_shortcode('[urlparam param="profile_pic"]'); ?><img src="<?php echo $src; ?>" /> Nov 28 '17 at 7:17
  • If they're in a template, and can use do_shortcode(), then they could just use $_GET[] to get the parameter directly and wouldn't need a shortcode, so I'm assuming the question is about the editor. Nov 28 '17 at 8:17
  • Awesome this worked!! Now how can I add this code to the wordpress editior? I am using using a page editior... ThriveThemes to be exact. Is it possible to turn this code into another shortcode? <?php $src = do_shortcode('[urlparam param="profile_pic"]'); ?><img src="<?php echo $src; ?>" /> Nov 28 '17 at 19:19
  • Try this: add_shortcode( 'my_shortcode', 'my_shortcode_callback' ); function my_shortcode_callback() { $src = do_shortcode('[urlparam param="profile_pic"]'); return "<img src='$src' />"; }, however please note that this looks like very weird code. There's a probably a better way to do what you want to do. I suggest you create another question where you explain what you're trying to achieve! Nov 29 '17 at 19:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.