2

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

/**
 * Creates a shortcode for shortcode_use_here
 * @author Omar Tariq <XXXXXX@gmail.com>
 */
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.

  • It works for me but that is a very peculiar shortcode. You are doing a lot of work to process that string. – s_ha_dum Jul 27 '15 at 1:36
  • @s_ha_dum The original shortcode where I experienced this problem is not only a string display. I made this small code that regenerates the error for me. After reading your comment, I downloaded wordpress again, pasted the shortcode code in twentyfifteen's functions.php. Made a new template and pasted the above mentioned page template code. Still, I got the same results. Screenshot: i.imgur.com/sJLhkN2.png – Omar Tariq Jul 27 '15 at 1:50
  • a herf is not valid HTML. – Aibrean Jul 28 '15 at 12:48
  • @Aibrean That's typo. Fixed. – Omar Tariq Jul 28 '15 at 22:17
7

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 );
  • 5
    "An expert is one who knows more and more about less and less" – Omar Tariq Jul 27 '15 at 5:08
  • Ha excellent !! – bonger Jul 27 '15 at 5:10
0

I cannot reproduce your problem but the way you are using the shortcode to construct small pieces of that string is making me crazy. Try this, and maybe it will work around your problem as well:

function callback_banana_abc( $args ){
  $atts = shortcode_atts( 
    array(
      'title' => 'yay!',
      'data' => 'yay!',
      'linktext' => 'yay!'
    ), 
    $args
  );
  $str = '<a href="#" title="'.$atts['title'].'" data-abc="'.$atts['data'].'">'.$atts['linktext'].'</a>';
  return $str; 
}
add_shortcode( 'banana_abc', 'callback_banana_abc' );

$str = '[banana_abc]';
echo do_shortcode($str);

$str = '[banana_abc title="ba" data="na" linktext="na"]';
echo do_shortcode($str);

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.