• I’ve already asked this question on wordpress.stackexchange.com and so far I got only one user to reply me and he said that he was unable to reproduce the error.

    My request to this sub-forum community is to test the following code, which can be a bug in WordPress. I can reproduce the error on my localhost machine as well as on the remote development server. I’m simply copy pasting the question to avoid wasting time.

    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 herf="#" title="[banana_abc]" data-abc="[banana_abc]">[banana_abc]</a>';
    echo do_shortcode($str);

    The output is:-

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

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

    The actual shortcode is much more complex. I’ve made it small and to the point to avoid wasting time.

    Please let me know if you can reproduce the error.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Mhh, that’s annoying?! I can reproduce the behavior and in a way I figured out why it does what it does. But I don’t have solution other than coding your own wrapper function to fix that.

    The function do_shortcode() is located in /wp-includes/shortcodes.php

    There, in line 209, the function do_shortcodes_in_html_tags( $content, $ignore_html ) is called – always.

    And so somewhere at the beginning of the function do_shortcodes_in_html_tags() all square brackets (that are left?) will be converted to their HTML special character codes.
    At this point your string would look like this: "<a herf="#" title="yay!" data-abc="& #91;banana_abc& #93;">yay!</a>" (without the space between & and #) and the shortcode in the middle won’t be recognized as such anymore.

    As I understand it the default behavior SHOULD be to prevent just all this from happening. The code comment says: “When true, all square braces inside elements will be encoded.” but the default is FALSE, so…?

    The one thing that worked is to kill line 209 in the shortcodes.php (or mark the line as comment // ). But of course, you shouldn’t edit core files!

    Also, if you do var_dump( do_shortcode( $str ) ); instead of echo do_shortcode($str); you can see that the square brackets have been replaced.

    Thread Starter ExortPress

    (@exortpress)

    Thanks Chris for your message. I found it’s solution yesterday. Here is the link:-

    https://wordpress.stackexchange.com/questions/195665/why-do-shortcode-is-not-executing-the-shortcode-in-data-html-attributes

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Why do_shortcode is not executing the shortcode in data-* html attributes?’ is closed to new replies.