5

I tried to include shortcodes with a parameter in raw html output, like shown below:

<a href="https://example.com/folder/edit.php?action=someaction&id=[foocode parameter='value']&edittoken=[foocode parameter='othervalue']">linktext</a>

This crashes the PHP function do_shortcode().

Is stuff like this really not possible with shortcodes?

The method description itself contains a warning:

Users with unfiltered_html * capability may get unexpected output if angle braces are nested in tags.

However, PHP crashing is not the kind of unexpected output that should be able to happen.

PS: The function that is being called is

function echocode( $atts ){
    return "Hello World";
}

and added as

add_shortcode("foocode", "echocode");

The function never runs. (No starting echocode is being printed)

2
5

shortcodes are not allowed in html attributes, shortcodes are not programing language, they are place holders to proper html content.

4
  • They actually are allowed, since the method has a switch to ignore shortcodes inside html tags or not. If they weren't allowed, the method should ignore all shortcodes inside of html tags for the sake of stability. – ApolloLV Oct 5 '16 at 15:24
  • 2
    if you are the expert why do you ask? :( they are not allowed, end of story. You can write your own content parsing if you want using whatever api you want, but if you ever intend a user to actually use it, it will fail. this is a feature of the 4.3 or 4.4 release, before that it was possible, and it was changed due to "abuses" of the type you show in your code. You might make it work, but this is not supported by core therefor you will never know when whatever you do will start failing – Mark Kaplun Oct 5 '16 at 15:28
  • Thank you, that was exactly the kind of insight I was looking for :D – ApolloLV Oct 5 '16 at 15:50
  • glad to help, don't take my grumpiness too hard ;) – Mark Kaplun Oct 5 '16 at 15:52
5

Hope this helps someone:

Instead of doing this: <a href="https://example.com/folder/edit.php?action=someaction&id=[foocode parameter='value']&edittoken=[foocode parameter='othervalue']">linktext</a>

You can do this: [foocode parameter1=value parameter2=othervalue] and then do this:

add_shortcode( 'foocode', 'prefix_foocode' );

function prefix_foocode( $atts ) {

    // Normalize $atts, set defaults and do whatever you want with $atts.

    $html = '<a href="https://example.com/folder/edit.php?action=someaction&id=' . $atts['parameter1'] .'&edittoken=' . $atts['parameter2'] . '">linktext</a>'
}
3

For what it's worth, shortcodes that don't accept any parameters appear to work in HTML tags. It's the ones that have parameters that don't.

Ex: <a href="https://example.com/folder/edit.php?action=someaction&id=[foocode parameter='value']&edittoken=[foocode parameter='othervalue']">linktext</a> doesn't work

Ex: <a href="https://example.com/folder/edit.php?action=someaction&id=[foocode]&edittoken=[foo-other-code]">linktext</a> does work (at least for me)

I have used a simple shortcode like this to output the path for images on my site so I don't have to remember the path each time (I don't use the media manager).

Hope this helps someone who may stumble on this question later.

2
  • 1
    It appears it's using the same quotes for the HTML attribute and the shortcode's attributes that create problems. <a href="[mytest attribute='1234']">test</a> works, as does <a href="[mytest attribute=1234]">test</a> or <a href='[mytest attribute="1234"]'>test</a>. – janh Jan 30 '18 at 8:57
  • Good to know. I don't remember if I tried this before, but it's possible that they changed or fixed something since last summer when I wrote this. Thanks for chiming in. – Crazycoolcam Jan 30 '18 at 12:55
2

Please have a try with this-

<a href="https://example.com/folder/edit.php?action=someaction&id=<?php echo do_shortcode("[foocode parameter='value']"); ?>&edittoken=<?php echo do_shortcode("[foocode parameter='othervalue']"); ?>">linktext</a>
2
  • 1
    This could work if coded into a file or function (function.php, a template, etc.), but would not work if coded into the TinyMCE editor as PHP automatically gets stripped out there. – Ian Mar 19 '17 at 4:37
  • @Ian exactly - the point of shortcodes is to let end-users (editors) add dynamic content in the editor; using php defeats the purpose altogether. – Peter Kionga-Kamau Dec 27 '18 at 18:56
0

For executing the shortcode inside the raw HTML element you would have to find the callback function that is being called via shortcode and then echo that function out in the raw html element.

For example if your shortcode is [foo_code], then find out which function is being called back when this function is being executed by finding the add_shortcode() for this shortcode (in this case [foo_code])-

add_shortcode('foo_code', 'foo_callback_function');

Now, when you know which function is being called you can simply echo this function in the raw html like:

[echo foo_callback_function();]

For more details, about why it works checkout this answer. Hope it helps!

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.