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.
最合适的回答,由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);