为什么DO_SHORT代码不执行data-*html属性中的快捷码?

时间:2015-07-27 作者:Omar Tariq

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 个回复
最合适的回答,由SO网友:bonger 整理而成

do_shortcodes_in_html_tags() 通过运行属性wp_kses_one_attr() 检查他们是否wp_kses_allowed_html( \'post\' ) 默认情况下,它只接受标准的非数据属性,因此您必须添加属性:

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

SO网友:s_ha_dum

我无法重现您的问题,但您使用短代码构造字符串小片段的方式让我发疯。试试这个,也许它也能解决你的问题:

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

结束

相关推荐

Apply_Filters(‘the_content’,$Content)与DO_ShortCode($Content)

假设我有一个主题选项或自定义的postmeta文本区域。现在我想执行多个短代码、一般文本、图像等。最佳实践是什么?为什么?选项1:$content = //my text area data; echo apply_filters(\'the_content\', $content); 选项2:$content = //my text area data; echo do_shortcode($content); 请告诉我哪一个是最佳实践,以及为什么。EDIT让我详细描