How do I use Shortcodes inside of HTML tags?

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)

5 s
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>';
return $html;
}

Leave a Comment