最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How do I use Shortcodes inside of HTML tags?

programmeradmin1浏览0评论

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

<a href=".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)

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)

Share Improve this question edited Oct 5, 2016 at 15:21 ApolloLV asked Oct 5, 2016 at 14:38 ApolloLVApolloLV 1511 gold badge1 silver badge4 bronze badges 2
  • 2 A shortcode callback must not echo anything, it must return a string. – fuxia Commented Oct 5, 2016 at 15:07
  • Even without echoing anything and the method just returning "", it still crashes. – ApolloLV Commented Oct 5, 2016 at 15:20
Add a comment  | 

5 Answers 5

Reset to default 6

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

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;
}

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.

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>

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!

发布评论

评论列表(0)

  1. 暂无评论