Why do_shortcode is not executing the shortcode in data-* html attributes?

This is on Vanilla installation. I’ve made a shortcode:-

/**
 * Creates a shortcode for shortcode_use_here
 * @author Omar Tariq <[email protected]>
 */
function callback_banana_abc( $args ){
    /* Don't echo! Always return */

    return 'yay!'; 
}
add_shortcode( 'banana_abc', 'callback_banana_abc' );

And I’ve made a template that looks like this:-

<?php
/*
 * Template Name: Test Template
 * Description: Hello world.
 */


$str="<a href="#" title="[banana_abc]" data-abc="[banana_abc]">[banana_abc]</a>";
echo do_shortcode($str);

The output is:-

<a href="#" title="yay!" data-abc="[banana_abc]">yay!</a>

This is only for data-* attributes. It works fine when used in title attribute.

2 Answers
2

do_shortcodes_in_html_tags() runs attributes through wp_kses_one_attr() which checks them against wp_kses_allowed_html( 'post' ) which by default only accepts standard non-data attributes, so you’d have to add your attribute:

add_filter( 'wp_kses_allowed_html', function ( $allowedposttags, $context ) {
    if ( $context == 'post' ) {
        $allowedposttags['a']['data-abc'] = 1;
    }
    return $allowedposttags;
}, 10, 2 );

Leave a Comment