Passing HTML in WordPress Shortcode arguments

I’ve searched a lot but didn’t find a solid answer for my question. So I’m posting a new one here. If someone know how to do it, please answer it with an explanation.

Lets say I have a shortcode function

    function some_random_function($atts, $content = null) {
       extract(shortcode_atts(array(
          'text' => null
       ), $atts));

       $return_string = '<div class="some-class">'. $text . $content .'</div>';
       return $return_string;
    }
add_shortcode( 'example', 'some_random_function' );

I know that this function does not make any sense that why I’m using $text there where I can simply use $content. But this is just a simple function do dont judge it.

Now the issue
Now if I pass [example text="lorem ipsum"]Lets roll[/example] the shortcode will work fine as all are normal string. But if I pass [example text="<a href="http://example.com">lorem</a> ipsum"]Lets roll[/example] it will not work.

So can anyone tell me how can I pass html in the shortcode arguments so that they work properly.

1 Answer
1

You have used the same quotation characters in 2 different contexts. You should write the shortcode in the following way:

[example text="<a href="http://example.com">lorem</a> ipsum"]Lets roll[/example]

Then, “unescape” HTML entities on the shortcode output:

$return_string = '<div class="some-class">'. html_entity_decode($text) . $content .'</div>';

Leave a Comment